Jump to content

More C++ Idioms/Policy Clone

From Wikibooks, open books for an open world

Policy Clone

[edit | edit source]

Intent

[edit | edit source]

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

Also Known As

[edit | edit source]

Motivation

[edit | edit source]

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 <class Apolicy>
class Host
{
  Apolicy direct_policy_use;
  Apolicy <SomeInternalType> InternalClone;  // Problem 1: Can't do this
};

template <class T, template <class T> class Apolicy>
class Host2
{
  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.
};

Solution and Sample Code

[edit | edit source]

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

template <typename T>
class NiftyAlloc
{
  public:
    template <typename Other>
    struct rebind // The Policy Clone idiom
    { 
       typedef NiftyAlloc <Other> other;
    };
    //...
};

template <typename T, class Alloc = NiftyAlloc <T> >
class Vector 
{
  public:
    typedef typename Alloc::template rebind<long>::other ClonePolicy;
    // 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.

Known Uses

[edit | edit source]
  • Standard Template Library
  • Compilers that do not support template template parameters
[edit | edit source]

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.

References

[edit | edit source]