1

I have a class template

template<class T>
class A
{
};

and one of its specialization

template<>
class A<B>
{
};

If C is a sub-class of B

class C : public B
{
};

Which instantiation is used for A<C>? If it uses the first one A<T>, how to let it use the second one A<B>?

user1899020
  • 13,167
  • 21
  • 79
  • 154

2 Answers2

1

The primary template will be used since A<B> is not a match for A<C>. Look at this question for suggestions on how to make it work: Template specialization based on inherit class

Community
  • 1
  • 1
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
1

Something along these lines:

template <typename T, bool isB>
class AHelper {
  // generic implementation
};

template <typename T>
class AHelper<T, true> {
  // specialization for B
};

template <typename T>
class A : public AHelper<T, std::is_base_of<B, T>::value>
{};
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85