tags:

views:

64

answers:

2

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

+3  A: 

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.

sth
I don't think that applies here. Note that the FAQ entry you cite refers to nested classes defined in the base, not member functions. Member functions have an implicit "this->" which would make them dependant by default.
James Curran
@James Curran: I changed the link to the next FAQ entry, which fits better. The problem is that the compiler doesn't know that `add()` refers to a member function. It might be a free function. Since `add` isn't a dependent name it is not searched in templated base classes, and therefore isn't found.
sth
One common mistake: The reason for not looking into base classes is not the state of dependency. It's the form of the name and name-lookup applied to it. Unqualified names during unqualified lookup are not looked up in dependent base classes. Even if they are dependent. This can easily happen - take `operator T()` for example, which is an unqualified dependent name, still not looked up in dependent bases. Note that the FAQ has it wrong - i mailed its author about it, and he agreed he needs to rework it.
Johannes Schaub - litb
Hi, thank you.Why doesn't look the compiler into the base class, when templated and not type name dependent? Is this a technical, standardization or speed reason?RegardsMartin
Martin Martinsson
+1  A: 

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 mehtods, which are allowed to resolve (use).

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

Martin

Martin Martinsson