More C++ Idioms/Policy Clone

= Policy Clone =

Intent
Instantiate a policy class with many different possible types without ad-hoc limitations on type of the policy classes.

Also Known As

 * Meta-function wrapper idiom

Motivation
Highly reusable, flexible and extensible classes can be built using policy based class design techniques. Sometimes, the host class of the policies needs to make an exact replica of one of its policies which is instantiated with a different type parameter. Unfortunately, the writer of the host class template does not know the template name to instantiate beforehand. Moreover, the policy class may or may not be a template in the first place. If it is a template, then the host class may not know how many minimum type parameters are required to instantiate the parameterized policy class. If the policy class is not a template, it may not be able to participate as a policy class. This situation is quite analogous to the situation in the Factory Method (GoF) pattern where type of the object to be created is not known a priori.

Solution and Sample Code
A member template struct (called rebind) is used to pass a different type parameter to the policy class template. For example,

Here, the Container template needs a replica of the allocation policy it is instantiated with. Therefore, it uses the rebind mechanism exposed by the NiftyAlloc policy. The type Alloc::template rebind ::other is same as NiftyAlloc. Essentially, it says, "I don't know what kind of allocator this type is, and I don't know what it allocates, but I want an allocator just like it that allocates longs." Using concepts（it has been removed from C++0x), the Vector class can write type concept that checks whether the Alloc policy type supports rebind concept.

To keep the compiler happy, we have to use both the keywords typename and template in the ClonePolicy typedef. The rule is as follows: If the name of a member template specialization appears after a ., ->, or :: operator, and that name has explicitly qualified template parameters, prefix the member template name with the keyword template. The Keyword typename is also necessary in the typedef because "other" is a type, not a variable.

Known Uses

 * Standard Template Library
 * Compilers that do not support template template parameters

Related Idioms
Meta-function wrapper idiom is a more powerful idiom than policy Clone. Policy Clone idiom indicates its purpose in a more abstract fashion than meta-function wrapper. The rebind template is essentially the meta-function wrapper.