我有嵌套模板及其模板專業化的問題。鑑於以下類:專用模板<typename T,模板<typename> class U>
小模板類
template<class U>
class T {
public:
T(){}
virtual ~T(){}
};
而且某種嵌套模板
template<typename T, template<typename> class U>
class A {
public:
void foo()
{
std::cerr << "A generic foo";
}
};
和少量的main.cpp
int main(int argc, const char *argv[])
{
A<int,T> *a = new A<int,T>;
a->foo();
//This wont work:
A<double,T*> *b = new A<double,T*>;
b->foo();
return 0;
}
現在我需要一個如果U是指針,則專精:
A<double,T*> *b = new A<double,T*>;
b->foo();
如何做到這一點?我想是這樣的:
template<typename T, template<typename> class U>
class A< T, U* >
{
public:
void foo()
{
std::cerr << "A specialized foo";
}
};
,但它只是在
A.h:18:16: Error: Templateargument 2 is invalid
您似乎很困惑。 – 2012-04-06 18:48:23
@ildjarn:不,這很好,因爲'A'需要一個具有一個參數的模板類型。 「T」符合該法案。 – bitmask 2012-04-06 19:08:42
@bitmask:啊,完全正確,我顯然沒有注意。 – ildjarn 2012-04-06 19:35:19