Question

I have a design like this:

template <class T>
class A
{
};

template <class T>
class B : public A<T> 
{
};

template <class T>
class C : public A<T> 
{
};

template <class T>
class D : public C<T>, public B<T> 
{
};

struct TConcrete {
int xyz;
};

class Concrete : public D<TConcrete>
{
void Foo();
};

void
Concrete::Foo()
{
Bar (boost::bind(&A::Afunc, this, _1, _2), boost::bind(&C::Cfunc, this, _1, _2),     boost::bind(&D::Dfunc, this, _1, _2));
}

The compiler complains about the first boost::bind call. The call to function inside C and D have no issues. Here is the exact error:

boost/bind/mem_fn_template.hpp(384) : error C2594: 'newline' : ambiguous conversions from 'Concrete *' to 'A *' with [ T=TConcrete ]

Any ideas what could be wrong with this?

Was it helpful?

Solution

Your inheritance diagram looks something like this:

           Concrete 
              |
              D
             / \
            C   B
           /     \
          A       A

When you try to convert your Concrete* into an A*, the compiler has no idea which instance of A you want. Do you want to convert to the A that C derives from, or the A that B derives from?

The solution is to use virtual inheritance to derive B and C from A, so that there is only one instance of A.

           Concrete 
              |
              D
             / \
            C   B
             \ /
              A
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top