2011-10-12 127 views
3

在另一個模板類中使用模板類參數的模板參數的正確語法是什麼?訪問模板類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. 

回答

6

你不能。模板模板參數表示您在不提供模板參數的情況下采用模板名稱

Foo<Param> tt; 

在這裏您可以看到沒有數值提供給Param。你需要一個模板模板參數,以便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輸出這些整數,它必須採取真正的類型,而不是模板。其次,模板參數的名稱(XY)只在範圍內有意義。它們完全是任意的標識符。你可以用簡單的元編程檢索值:

#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; 
} 
0

你不能把模板類參數的模板參數,因爲參數是不帶參數的模板。你可以這樣做,而不是:

template < typename X1 > 
struct Foo; 

template < template < int, int > class X1, int A, int B > 
struct Foo< X1< A, B > > 
{ 
    ... here you can use A and B directly, or X1< A, B >::X and X1< A, B >::Y ... 
}; 

您指定模板採取單一類型,並專注它採用兩個int參數的模板的情況下。有了這個定義,你可以使用這樣的:

Foo< Param< 0, 1 > > tt; 
1

這這裏是可以工作,以及一個例子:

template < typename X1> 
struct Foo; 

template < template < int, int > class X1, int X, int Y > 
struct Foo< X1<X,Y> > { 
    int foo() { 
     printf("ok%d %d\n", X, 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<30,40> > tt; 
    tt.foo(); 
    return 0; 
} 
相關問題