在另一個模板類中使用模板類參數的模板參數的正確語法是什麼?訪問模板類A中的X和Y,如模板<template <int X,int Y> class> class A;
例如:如何訪問Foo類中的Param的X和Y?
計劃:
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;
}
這樣上面的代碼,爲printf語句編譯器抱怨:
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.