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.

template<classApolicy>classHost{Apolicydirect_policy_use;Apolicy<SomeInternalType>InternalClone;// Problem 1: Can't do this};template<classT,template<classT>classApolicy>classHost2{Apolicy<T>common_use;Apolicy<SomeInternalType>InternalClone;// Can do this now but // Problem 2: policies that require more than one type parameter can't participate.};

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

template<typenameT>classNiftyAlloc{public:template<typenameOther>structrebind// The Policy Clone idiom{typedefNiftyAlloc<Other>other;};//...};template<typenameT,classAlloc=NiftyAlloc<T>>classVector{public:typedeftypenameAlloc::templaterebind<long>::otherClonePolicy;// Here, Alloc may not be a template class or a parametrized instantiation of// a class that takes unknown number of type parameters.};

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<long>::other is same as NiftyAlloc<long>. 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.

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.