简体   繁体   中英

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 ? Is it possible to differ std::vector<A,B> from 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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