Why does "using MyBase::myMethod" solve "request for member myMethod is ambiguous" ? (NOT diamond pattern!!!)

StackOverflow https://stackoverflow.com/questions/17083340

Question

The Background: I offer a virtual file structure over two different technologies: FUSE and MTP. Since both frameworks need different interfaces, I ve created two base classes which serve these interfaces. The FUSE famework only knows the IFuseFile interface while the MTP framework only knows the IMTPFile interface. These base classes have pure virtual methods, which are implemented by the derived class.

The Problem: When implementing it straight forward, I get a "request for member IsWriteable is ambiguous" by the compiler (see example source).

The Solution: When searching for a solution, I only found the diamond pattern. But I have only common pure virtual methods, no common classes. For me a simple using BASE::method does the trick.

The Question: Since I used the using BASE::method only for hidden methods before, I cannot explain why this code solves my problem. Can you explain it? Is this only a GCC bug/feature?

The Example:

class IFuseFile
{
    virtual bool IsWriteable() const = 0;
public:
    int HandleReadRequest( struct fuse_data* pData )
    {
        if( !IsWriteable() ) return -EACCESS;
        ...
    }
}
class IMTPFile
{
    virtual bool IsWriteable() const = 0;
public:
    int ReadData( const char* pBuffer, int iSize )
    {
        if( !IsWriteable() ) return -1;
        ...
    }
}
class PcComFile : public IFuseFile, public IMTPFile
{
    using IFuseFile::IsWriteable;
    using IMTPFile::IsWriteable;
}
class LogFile : public PcComFile
{
    bool IsWriteable() const override { return true; }
}
class StatusFile : public PcComFile
{
    bool IsWriteable() const override { return true; }
}
Était-ce utile?

La solution

If you don't have a using declaration in PcComFile, and you try to access IsWriteable through a PcComFile reference or pointer, the compiler doesn't know which one you want. It doesn't matter that it makes no difference; C++'s name lookup and ambiguity rules do not take into account that both functions are abstract.

A using declaration for one of the methods disambiguates the request and it's not relevant which one you pull in while both methods are abstract. Having two using declarations is definitely redundant, and actually looks wrong to me. It looks like it should fail to compile or make it ambiguous again.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top