Valid
	XHTML 1.1! Valid CSS!
Created 2006-07-18   Modified 2009-04-11
Chelton Evans

proj Override Default Template Arguments home

Intro
Example
Details
Source
References

Intro

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.

Example

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;

Details

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;
...
};

Source

Files

Makefile
main.cpp
policydefault.cpp
policydefault.h
policyselector.h

projcompile.txt
unittestsreport.txt

Doxygen

main.cpp
Makefile
policy1
policy2
policy3
policy4
policy5
policydefault
policyov1
policyov2
policyov3
policyselector

References

  1. D.Vandevoorde and N.M.Josuttis (2003). C++ Templates The Complete Guide. ISBN 0-201-73484-2. Page 286.