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.
#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
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
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