Specialization with automatic inheritance

I am trying to reduce code duplication / verbosity as much as I can. What I'm trying to accomplish is something like this:

template<class A, class B>
  struct Base
    {
    void g() { std::cout << "generic derived";}
    };

  template<class T, class V>
  struct Derived : Base<T,V>
    {
    void f() { std::cout << "generic derived";}
    };

  template<class T>
  struct Derived<T, int> : Base<T,int>
    {
    // we inherit Base::g()
    void f() { std::cout << " derived for int";}
    };

  template<class T>
  struct Derived<T, double> // Here inheritance 
    {
    // we do not inherit Base::g()
    void f() { std::cout << " derived for double";}
    };

      

I would like to reduce the verbosity of the code by avoiding somehow repeating: Base for each specialization. Do you know any boilerplate trick I can use to solve this "problem"? which allows you to automatically inherit base functionality or whatever uses the template using alias keyword.

thank

[edit] inheritance for double, omitted to show that there is no Base :: g () for double

[edit2] just wondering if the following solution might work:

  template<class A>
  struct Base {};

  template<class A, class B>
  struct ToSpecialize{};

  template<class A, class B>
  struct ToUse : ToSpecialize<A, B>, Base<A> {};

      

so I just need to specialize ToSpecialize and use ToUSe. I could also specialize Base if needed

+3


source to share





All Articles