2016-03-15 147 views
1

我有一个模板类如何专门为一个类的模板模板参数?

template <class T> 
struct TypeText { 
    static const char *text; 
}; 

和一些专业化的为“文本”成员:

template <> const char* TypeText<int>::text = "INT"; 
template <> const char* TypeText<long>::text = "LONG"; 

如何贯彻std::vector<A,B>没有先验知识约AB专业化?是否有可能从SomeOtherClass<A,B>不同std::vector<A,B>

下不起作用:

template <> 
template <class T, class A> 
const char* TypeText< std::vector<T,A> >::text = "vector"; 

回答

2

你可以提供一个partial template specializationstd::vector

template <class T> 
struct TypeText<std::vector<T>> { 
    static const char *text; 
}; 
template <class T> 
const char* TypeText<std::vector<T>>::text = "vector"; 

然后使用它,例如:

...TypeText<std::vector<int>>::text... // "vector" 
...TypeText<std::vector<long>>::text... // "vector" 

LIVE

+0

您已更改初始模板类。 – pavelkolodin

+0

我错了。这是部分专业化让我感到惊讶。谢谢。 – pavelkolodin

+0

@pavelkolodin BTW:你为什么要指定'std :: vector'的第二个模板参数?如果你真的需要这样做,你也可以为局部特化添加第二个模板参数。 – songyuanyao