Solving base class ambiguity for nested template parameter functions

  ambiguous, c++, inheritance, templates

I have classes foo_impl, bar_impl deriving from derived classes foo_derived, bar_derived, and a function that takes in a templated template-parameter:

#include <iostream>
template <typename T>
struct foo_base
{
    T a;
};

template <typename T>
struct foo_derived: public foo_base<T>
{

};

struct foo_impl: public foo_derived<int>
{

};

template <typename T>
struct bar_base
{
    T a;
};

template <typename T>
struct bar_derived : public bar_base<T>
{
};

struct bar_impl : public bar_derived<int>
{
};

template <typename T, template <class> typename Base>
void useBase(const Base<T>& Arg)
{
    std::cout << Arg.a << std::endl;
}

int main()

{
    foo_impl foo;
    bar_impl bar;

    useBase(foo); // ‘const Base<T>’ is an ambiguous base class of ‘foo_impl’
    useBase(bar); // ‘const Base<T>’ is an ambiguous base class of ‘bar_impl’
}

Is it possible to specify that I want foo_base and bar_base to be the only types that can be passed to useBase(.)?

Source: Windows Questions C++

LEAVE A COMMENT