2

I'm attempting to work with a third-party library which makes use of something similar to the following approach:

struct A {
    virtual A* clone();
};

struct B1: public virtual A {
    B1* clone() override;
};

struct B2: public virtual A {
    B2* clone() override;
};

struct C: public B1, public B2 {
    C* clone() override;
};

GCC and Clang have no issues with this, but the MSVC compiler chokes: error C2250: 'C': ambiguous inheritance of 'B1 *A::clone(void)'

To my understanding, this construct is valid under the C++ standard: a C* is properly covariant with the pointers to any of the base classes, the definition of C::clone() should override (or hide) any definition of clone() in the base classes for C itself, and the rules for which function body gets called and what type should be returned is well defined for any point-of-use of obj.clone().

I have found a Stack Overflow answer from 2011 which claims it is a known bug in MSVC, which doesn't help me much. (Especially since the context is now a dead link.)

As mentioned, this is a third-party library. Their primary target compilers are GCC & Clang. (Ours are too, but we also need to support MSVC.) While they'll probably be receptive to small patches to get things to work with MSVC, suggesting radical rewriting just because MSVC is being difficult is a non-starter. (I looked, and both multiple inheritance and the covariant return types here are baked pretty deeply into the design of the library.) It's also a specialized library with unique functionality, so "find a different library" is also not going to work.

Is there any way to convince Microsoft Visual C++ to actually compile this code? Or is there some other way to enable this functionality in a MSVC-compatible way without a massive re-write of the library?

Christophe
  • 68,716
  • 7
  • 72
  • 138
R.M.
  • 3,461
  • 1
  • 21
  • 41

0 Answers0