简体   繁体   English

如何为一个类专门化模板template参数?

[英]How to specialize template template parameter for a class?

I have a template class 我有一个模板课

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

and a couple of specializations for the "text" member: 以及“文本”成员的几个专业化:

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

How to implement a specialization for std::vector<A,B> with no prior knowledge about A and B ? 如何在没有有关AB先验知识的情况下为std::vector<A,B>实现专门化? Is it possible to differ std::vector<A,B> from SomeOtherClass<A,B> ? 是否可以将std::vector<A,B>SomeOtherClass<A,B>

The following doesn't work: 以下内容不起作用:

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

You could provide a partial template specialization for std::vector : 您可以为std::vector提供部分模板专业化

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

then use it such as: 然后使用它,例如:

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

LIVE 生活

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM