I have the following code snippet:
typedef int AliasB; typedef unsigned short AliasA; class Alias { public: explicit Alias(int someInt) { } }; // (*) !! below breaks the conversion path via AliasA !! //typedef Alias AliasA; class C { public: C() { } }; class B { public: B() { } B(const AliasB& value) { } operator AliasB() const { return -1000; } C combine(const B& someB) { return C(); } }; class A { public: A() { } operator B() const { return B(); } operator AliasA() const { return 1001; // (*) !! below breaks the conversion path via AliasA !! //return AliasA(1000); } A high() { return A(); } A low() { return A(); } C process() { return (static_cast<B>(low())).combine(static_cast<B>(high())); // (**) !! the below compiles fine !! //B theB = low(); //return theB.combine(high()); } }; inline int someFunc(unsigned int someParam, const B& bParam) { return 1; } inline A createSomeA() { return A(); } int main () { A someA; unsigned int counter = 200; someFunc(counter, someA); //someFunc(counter, static_cast<B>(createSomeA())); someA.process(); return 0; }
Clang reports the following error:
clang_static_test.cpp:66:17: error: ambiguous conversion for static_cast from 'A' to 'B' return (static_cast<B>(low())).combine(static_cast<B>(high())); ^~~~~~~~~~~~~~~~~~~~~ clang_static_test.cpp:21:7: note: candidate is the implicit copy constructor class B ^ clang_static_test.cpp:25:5: note: candidate constructor B(const AliasB& value) { } ^ clang_static_test.cpp:66:48: error: ambiguous conversion for static_cast from 'A' to 'B' return (static_cast<B>(low())).combine(static_cast<B>(high())); ^~~~~~~~~~~~~~~~~~~~~~ clang_static_test.cpp:21:7: note: candidate is the implicit copy constructor class B ^ clang_static_test.cpp:25:5: note: candidate constructor B(const AliasB& value) { } ^ 2 errors generated.
I can’t understand why the compiler generates an error, although I have a conversion operator, and I make the conversion in this particular place explicit using static_cast <>. The code passes the compilation with the GCC 4.5.2 and Visual Studio 2008 compilers. The Clang version is 3.1, which I built from the git Clang and LLVM repositories a couple of days ago.
So, is Clang reporting an actual error? And if so, why is this a mistake, because (I will not ask why other compilers are silent about this)?
UPDATE: The sample code is now a small compiled example (sorry for not doing it the first time) and replicating the real situation that I have. It seems that the conversion operator in AliasA is a problem, because if it is deleted, everything compiles fine. Now the unpleasant thing is that for the above code snippet, I get errors also from GCC.
UPDATE 2 : I added code to the sample to better reflect my real situation; the only difference is that for the above sample I also get an error message from GCC, whereas for my real code I do not.
source share