What is the correct syntax to use template parameters of a template class argument in another template class?
For example: How can I access X and Y of class Param in class Foo?
Program:
template < template < int, int > class X1>
struct Foo {
int foo() {
printf("ok%d %d\n", X1::X, X1::Y);
return 0;
}};
template < int X, int Y >
class Param {
int x,y;
public:
Param(){x=X; y=Y;}
void printParam(){
cout<<x<<" "<<y<<"\n";
}
};
int main() {
Param<10, 20> p;
p.printParam();
Foo< Param > tt;
tt.foo();
return 0;
}
As such for the above code, for the printf statement compiler complains:
In member function 'int Foo<X1>::foo()':
Line 4: error: 'template<int <anonymous>, int <anonymous> > class X1' used without template parameters
compilation terminated due to -Wfatal-errors.
You can’t. The template template parameter means that you take a template name without supplied template arguments.
Here you can see that no values are supplied for
Param. You’d take a template template parameter so that Foo itself could instantiateParamswith any arguments it likes.Example:
But if you want your
Footo output those integers, it has to take real types, not templates. Secondly, the names of the template arguments (XandY) are only meaningful where they are in scope. They are otherwise completely arbitrary identifiers. You can retrieve the values with simple metaprogramming: