Override Default Template Arguments
Intro
Example
Details
Source
References
Vandevoorde and Josuttis [1] overload the typedef to provide client support for overriding default template arguments.
I have come up with an even simpler technique. No virtual inheritance is used.
However their instantiation has a simpler calling convention. I use a recursive calling convention.
Let the problem have 6 template arguments for a real example.
// Actual instantiation. PolicySelector<Policy3_is<CustomPolicy> > app2;
Here is my equivalent code. Consider that this is MUCH easier to implement than their technique. It uses a recursive calling convention instead of their linear calling convention. For overloading one template argument there is no notational difference.
policyselector< policyov3< CustomPolicy > > app;
Consider when two template policies are overloaded, first theirs then mine.
PolicySelector<Policy5_is<CustPol2>,Policy3_is<CustomPolicy> > app;
Mine
policyselector< policyov5< policyov3< CustomPolicy > > > app;
Consider the change to just one typedef. This is ofcourse repeated for the other typedef's.
The typedef P1 is overloaded through inheritance.
template< typename P, typename PI=policydefault >
class policyov1 : public PI
{
public:
typedef P P1;
};
The policy selector realizes the typedef arguments to instantiate the choices.
template < typename T=policydefault >
class policyselector : public T
{
public:
/** The first policy. */
typename T::P1 p1;
/** The second policy. */
typename T::P2 p2;
/** The third policy. */
typename T::P3 p3;
...
};