http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58052
Bug ID: 58052 Summary: Copy initialization using conversion operator does not find correct candidates for initialization of final result Product: gcc Version: 4.8.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: hstong at ca dot ibm.com I have a reduced test case. MSVC seems to work fine (online compiler test: http://rise4fun.com/Vcpp/2sQ). The copy initialization of an object of type A from a class object of type C is expected to find C::operator B &() as the function selected by overload resolution. The result of the call, an lvalue of type B, is then used to direct-initialize the object that is the destination of the copy-initialization. See C++11 subclause 8.5 [dcl.init] paragraph 16. Note that the result of the call is specified to be used, not the result of the user-defined conversion sequence which was considered for overload resolution. The direct initialization from the lvalue of type B has for its candidates all of the constructors for A (13.3.1.3 [over.match.ctor]). Note that A(B &) has a standard conversion sequence from the lvalue of type B to its sole argument (the identity conversion). GCC seems to be fixated with the copy constructors for A instead of performing the overload resolution for the direct initialization. ## Self-contained test case (main.cpp): struct B; struct A { A(); A(const A &, bool = 0); A(const A &, short = 0); A(B &); }; struct B : A { }; struct C { operator B &(); }; int main() { C c; A a = c; } ## Compiler invocation: g++ -std=c++11 -c main.cpp ## Compiler output: main.cpp: In function 'int main()': main.cpp:17:10: error: call of overloaded 'A(A)' is ambiguous A a = c; ^ main.cpp:17:10: note: candidates are: main.cpp:5:4: note: A::A(const A&, short int) A(const A &, short = 0); ^ main.cpp:4:4: note: A::A(const A&, bool) A(const A &, bool = 0); ^ ## Expected behaviour: Clean compile. ## g++ -v: Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.1-2ubuntu1~12.04' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~12.04)