开发者

Inherit from specialized templated base class and call the right method

开发者 https://www.devze.com 2023-01-13 00:23 出处:网络
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.

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 > :
       In开发者_StackOverflowstruction<Int2Type<Add_Type>, Base >
{
   void mul() 
   {
      add(); ???? <== not working (not resolved)
   }
};

What's the solution for this?

Thank you

Martin


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号