If foo()
doesn't depends from other elements of A
, you can inherit it through a base class and specialize the base class.
I mean something as follows
template <typename>
struct Base
{ void foo() { std::cout << "A<V>
"; } };
template <typename ... Ts>
struct Base<std::vector<Ts...>>
{ void foo() { std::cout << "A<V<Ts...>>
"; } };
template <typename T>
struct A : public Base<T>
{ };
Another possible solution is tag-dispatching: develop two foo()
functions an "enable" the correct one, according the needs.
For example
template <typename>
struct is_vector : public std::false_type
{ };
template <typename ... Ts>
struct is_vector<std::vector<Ts...>> : public std::true_type
{ };
template <typename T>
struct A
{
void foo (std::true_type) { std::cout << "A<V<Ts...>>
"; }
void foo (std::false_type) { std::cout << "A<V>
"; }
void foo () { foo(is_vector<T>{}); }
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…