I l@ve RuBoard |
![]() ![]() |
1.8 Optional Functionality Through Incomplete InstantiationIt gets even better. C++ contributes to the power of policies by providing an interesting feature. If a member function of a class template is never used, it is not even instantiated—the compiler does not look at it at all, except perhaps for syntax checking.[5]
This gives the host class a chance to specify and use optional features of a policy class. For example, let's define a SwitchPrototype member function for WidgetManager. // Library code template <template <class> class CreationPolicy> class WidgetManager : public CreationPolicy<Widget> { ... void SwitchPrototype(Widget* pNewPrototype) { CreationPolicy<Widget>& myPolicy = *this; delete myPolicy.GetPrototype(); myPolicy.SetPrototype(pNewPrototype); } }; The resulting context is very interesting:
This all means that WidgetManager can benefit from optional enriched interfaces but still work correctly with poorer interfaces—as long as you don't try to use certain member functions of WidgetManager. The author of WidgetManager can define the Creator policy in the following manner:
In conjunction with policy classes, incomplete instantiation brings remarkable freedom to you as a library designer. You can implement lean host classes that are able to use additional features and degrade graciously, allowing for Spartan, minimal policies. |
I l@ve RuBoard |
![]() ![]() |