pavelkolodin
pavelkolodin

Reputation: 2967

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";

Upvotes: 1

Views: 48

Answers (1)

songyuanyao
songyuanyao

Reputation: 172924

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

Upvotes: 2

Related Questions