Martin Martinsson
Martin Martinsson

Reputation: 23

Inherit from specialized templated base class and call the right method

I.e. I got 2 specialized types of:

template <class Type, class Base> struct Instruction {};

to compile-time-select the appropriate type from within a type list.

like:

template <class Base> struct Instruction<Int2Type<Add_Type>, Base > 
{
   void add() {}
};

template <class Base> struct Instruction<Int2Type<Mul_Type>, Base > :
       Instruction<Int2Type<Add_Type>, Base >
{
   void mul() 
   {
      add(); ???? <== not working (not resolved)
   }
};

What's the solution for this?

Thank you

Martin

Upvotes: 1

Views: 379

Answers (2)

Martin Martinsson
Martin Martinsson

Reputation: 23

Got it using a "using base method" clause like:

using base::add;

One disadvantage is to declare that using clauses for all base methods. On advantage is the implicit distinctive nature of it selecting only that methods, which are allowed to resolve (use).

Sorry for that question of a dude which forgets his head somewhere.

Upvotes: 1

sth
sth

Reputation: 229583

add() doesn't appear to depend on any template parameters (it's not a "dependent name"), so the compiler doesn't search for it in the templated base class.

One way to make it clear to the compiler that add() is supposed to be a member function/dependent name, is to explicitly specify this-> when you use the function:

void mul() 
{
   this->add();
}

this implicitly depends on the template parameters, which makes add a dependent name that is looked up in the templated base class.

See also this entry of the C++ FAQ lite, and the next/previous ones.

Upvotes: 3

Related Questions