3

I am trying to reduce code duplication / verbosity as much as I can. What I am 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'd like to reduce the verbosity of the code by avoiding in some way to repeat the : Base for each specialization. Are you aware of any template trick that I can use to solve this "problem" ? something that allows to automatically inherit the base functions or something that makes use of the using alias template keyword..

Thanks

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

[edit2] just wondering if the following solution could 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 that I just need to specialize ToSpecialize and use ToUSe. I could also specialize Base if needed

user3770392
  • 453
  • 5
  • 12

0 Answers0