1

I want to have a general storage class that can store variety of objects, I don't want to use a heterogeneous container class that stores all of them. I thought of creating a templated storage class, and create a general storage class that inherits from this metaclasses with different types:

template<typename Type>
struct SingleInterface
{
public:
    SingleInterface() = default;
    virtual ~SingleInterface() = default;
    SingleInterface(const SingleInterface &) = default;
    SingleInterface & operator=(const SingleInterface &) = default;
    SingleInterface(SingleInterface &&) = default;
    SingleInterface & operator=(SingleInterface &&) = default;

    void get(const std::string & keyword, Type * ptr)
    {
        // implementation
    }

};

My general class as below:

class MutliInterface: public SingleInterface<double>, SingleInterface<int>
{
public:
    MutliInterface() = default;
    virtual ~MutliInterface() = default;
};

when I create a MutliInterface class, I get the following error:

MutliInterface interface;
double *v;
interface.get("test", v);

'get' is ambiguous '

Difster
  • 3,264
  • 2
  • 22
  • 32
apramc
  • 1,346
  • 1
  • 15
  • 30
  • doesn't Type in get method solve the ambiguity ??? void get(const std::string & keyword, Type * ptr) – apramc Aug 10 '17 at 18:14

1 Answers1

4

Expose both overloads with a using statement in the derived class:

class MutliInterface: public SingleInterface<double>, SingleInterface<int>
{
public:
    MutliInterface() = default;
    virtual ~MutliInterface() = default;
    using SingleInterface<double>::get;
    using SingleInterface<int>::get;
};

It's ambiguous because of [class.member.lookup] that basically says that when the name of a function get isn't found immediately in the class MutliInterface, a set of candidate functions is created using the class' base classes. However, the functions must be identical otherwise it's ambiguous (and here they are NOT identical).

When we use the using keyword here, we shortcut all that and both overloads are immediately accessible from the derived class.

AndyG
  • 39,700
  • 8
  • 109
  • 143