0

I am attempting to create template classes where each can solve a specific facet of the problem so to be able to mishmash them without resorting to creating the traditional abstract virtual classes. For that, I believe CRTP would be the best paradigm. However, when using CRTP a bit more I found trapped on this weak resolution logic - compiler (g++ 4.8.2) cannot distinguish between two methods on different classes even though their signature is different - only the method name is the same.

The classes implementation:

template< class T >
class A {
public:
    void foo( uint32_t val ) {
        T* me = static_cast<T*>( this );
        me->doit();
    }
};

template< class T >
class B {
public:
    void foo() {
        uint32_t v32 = 10;
        T* me = static_cast<T*>( this );
        me->foo( v32 );
    }
};

class Derived : public A<Derived>,
                public B<Derived>
{
public:
    void doit() {
        std::cout << "here" << std::endl;
    }
};

Then it is used as

Derived d;
d.foo();

When compiled, this error surfaces:

$ g++ -std=c++11 -c testLambda.cpp

testLambda.cpp: In function ‘int main(int, char**)’:
testLambda.cpp:102:7: error: request for member ‘foo’ is ambiguous
 d.foo();
   ^
testLambda.cpp:25:10: note: candidates are: void B<T>::foo() [with T = Derived]
    void foo() {
      ^
testLambda.cpp:16:10: note:                 void A<T>::foo(uint32_t) [with T = Derived; uint32_t = unsigned int]
     void foo( uint32_t val ) {

Is this a compiler bug or the actual expected result?

  • User [pubby8](http://www.reddit.com/u/pubby8) at reddit.com/r/cpp [responded](http://www.reddit.com/r/cpp/comments/384j9g/why_g_cannot_resolve_the_scope_of_this_apparently/crs87nj) (quote) a quick fix is to add this to Derived's class body: using A::foo; using B::foo; –  Jun 03 '15 at 05:42

1 Answers1

0

User pubby8 at reddit.com/r/cpp responded (quote) a quick fix is to add this to Derived's class body:

using A<Derived>::foo;

using B<Derived>::foo;