Saturday, November 27, 2010

Nested Templates

You can have nested templates.

template <class U, class V> class A {
public: template <class V> V function() {return V();} };

Note the the first 'class V' is unrelated to the second 'class V'. It is like having the second 'class V' being 'class W'. To fully instantiate the template, you to provide for class U, class V, and class V again. The following program shows a usage:

int main () {
   A<int,int> a;
   cout << a.function<float>(); 
   return 0;

No comments:

Post a Comment