Вы не можете. Параметр шаблона шаблона означает, что вы берете имя шаблона без предоставленных аргументов шаблона .
Foo< Param > tt;
Здесь вы можете видеть, что для Param
не указаны значения. Вы бы взяли параметр шаблона template, чтобы сам Foo мог создать экземпляр Params
с любыми аргументами, которые ему нравятся.
Пример:
template < template < int, int > class X1>
struct Foo {
X1<1, 2> member;
X1<42, 100> foo();
};
template <int N, int P> struct A {};
template <int X, int Y> struct B {};
Foo<A> a_foo; //has a member of type A<1, 2>, foo returns A<42, 100>
Foo<B> b_foo; //has a member of type B<1, 2>, foo returns B<42, 100>
Но если вы хотите, чтобы ваш Foo
выводил эти целые числа, он должен принимать реальные типы, а не шаблоны. Во-вторых, имена аргументов шаблона (X
и Y
) имеют смысл только там, где они находятся в области видимости. В остальном это совершенно произвольные идентификаторы. Вы можете получить значения с помощью простого метапрограммирования:
#include <cstdio>
template <class T>
struct GetArguments;
//partial specialization to retrieve the int parameters of a T<int, int>
template <template <int, int> class T, int A, int B>
struct GetArguments<T<A, B> >
{
enum {a = A, b = B};
};
//this specialization also illustrates another use of template template parameters:
//it is used to pick out types that are templates with two int arguments
template <class X1>
struct Foo {
int foo() {
printf("ok%d %d\n", GetArguments<X1>::a, GetArguments<X1>::b);
return 0;
}
};
template < int X, int Y >
class Param {
public:
void print();
};
//this is to illustrate X and Y are not essential part of the Param template
//in this method definition I have chosen to call them something else
template <int First, int Second>
void Param<First, Second>::print()
{
printf("Param<%d, %d>\n", First, Second);
}
int main() {
Foo< Param<10, 20> > tt;
tt.foo();
Param<10, 20> p;
p.print();
return 0;
}