public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/58052] New: Copy initialization using conversion operator does not find correct candidates for initialization of final result
@ 2013-08-02  4:45 hstong at ca dot ibm.com
  2013-08-02 13:51 ` [Bug c++/58052] " hstong at ca dot ibm.com
  2021-08-09 19:43 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: hstong at ca dot ibm.com @ 2013-08-02  4:45 UTC (permalink / raw)
  To: gcc-bugs

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)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/58052] Copy initialization using conversion operator does not find correct candidates for initialization of final result
  2013-08-02  4:45 [Bug c++/58052] New: Copy initialization using conversion operator does not find correct candidates for initialization of final result hstong at ca dot ibm.com
@ 2013-08-02 13:51 ` hstong at ca dot ibm.com
  2021-08-09 19:43 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: hstong at ca dot ibm.com @ 2013-08-02 13:51 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58052

--- Comment #2 from Hubert Tong <hstong at ca dot ibm.com> ---
(In reply to Paolo Carlini from comment #1)
> Not having spent a lot of time on this, I doubt it's a bug: the latest clang
> and icc behave exactly like gcc.

I had a vague recollection why I found the code weird to begin with.
After consideration, I found that the MSVC behaviour made sense after all.

There is wording in 12.3 [class.conv]:
At most one user-defined conversion (constructor or conversion function) is
implicitly applied to a single value.

To poke some holes in the applicability of the above wording, I will modify the
test case:
@@ -3,7 +3,8 @@ struct A {
    A();
    A(const A &, bool = 0);
    A(const A &, short = 0);
-   A(B &);
+
+   template <typename T> explicit A(T &);
 };

 struct B : A { };

Note that explicit constructors are not converting constructors.

The modified version of the test case works fine with clang++ and icc, but not
GCC. MSVC chokes on the "explicit".


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/58052] Copy initialization using conversion operator does not find correct candidates for initialization of final result
  2013-08-02  4:45 [Bug c++/58052] New: Copy initialization using conversion operator does not find correct candidates for initialization of final result hstong at ca dot ibm.com
  2013-08-02 13:51 ` [Bug c++/58052] " hstong at ca dot ibm.com
@ 2021-08-09 19:43 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-09 19:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58052

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
GCC and ICC both reject this while clang and MSVC accepts this.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-09 19:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-02  4:45 [Bug c++/58052] New: Copy initialization using conversion operator does not find correct candidates for initialization of final result hstong at ca dot ibm.com
2013-08-02 13:51 ` [Bug c++/58052] " hstong at ca dot ibm.com
2021-08-09 19:43 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).