Meditation, The Art of Exploitation

Thinking? At last I have discovered it--thought; this alone is inseparable from me. I am, I exist--that is certain. But for how long? For as long as I am thinking. For it could be, that were I totally to cease from thinking, I should totally cease to exist....I am, then, in the strict sense only a thing that thinks.

Thursday, August 03, 2006

template template argument

The following code snippet demonstrates a neat feature of C++, template template argument. C++ template parameter list can contain a declaration of template as shown in this example. The primary reason of using template tempalte argument is to coordinate template arguments. For example, to instantiate an object of A, one needs to write "A< int, std::allocator, std::vector > a". The composite design (has-a) of class A will be implemented through std::vector< int, std::allocator >.

#include < vector >
#include < iostream >

template < typename T, class Alloc, template < typename, typename> class C>
struct A{
C< T, Alloc > c;
A(){ c[0] = 10; }
void print(){ std::cout << "A" << std::endl; }
};

int main(){
A< int, std::allocator, std::vector > a;
a.print();
}

To further illustrate the point, one can completely remove the dependence of allocator on container type by rewriting the code like this:

#include < vector >
#include < iostream >

template < typename T, template < typename> class Alloc, template < typename, typename> class C>
struct A{
C< T, Alloc > c;
A(){ c[0] = 10; }
void print(){ std::cout << "A" << std::endl; }
};

int main(){
A< int, std::allocator, std::vector > a;
a.print();
}

In this final example, there is no room for error or confusion (one cannot write
A< int, std::allocator, std::vector> a). The intention of the code is clear and self consistent.