Class partial specialization inheriting methods

Viewed 57
template <unsigned N>
class A {
    void common_function();
};

template <>
class A<1> {
    void new_functionality();
};

I want class A<1> to inherit common_function() from A class.

This would be useful if you need to add some functionality to classes partial specialization, but don't want to duplicate common methods.

1 Answers

Even though you say partial specialization, this is actually an explicit specialization. Not that it matters either way as both explicit and partial specializations are separate types from the base template.

The way to achieve what you want is to introduce another type (likely not a template at all) to deal with the shared functionality, and derive both your base template and the specializations from that type.

struct shared
{
  void common_function() { ... } 
};

template<unsigned N>
class a : public shared
{ void foo() { std::cout << "a<N>::foo" << std::endl; }
};

template<>
class a<1> : public shared
{
  void foo() { .std::cout << "a<1>::foo" << std::endl; }
};
Related