Bug 54521

Summary: g++ fails to call explicit constructors in the second step of copy initialization
Product: gcc Reporter: Nikolka <tsoae>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: daniel.kruegler
Priority: P3 Keywords: rejects-valid
Version: 4.7.1   
Target Milestone: 6.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2012-09-07 00:00:00

Description Nikolka 2012-09-07 20:38:12 UTC
g++ fails to compile the following code

    struct X
    {
        X(int) {}
        explicit X(X const &) {}
    };

    int main()
    {
        X x = 1; // error: no matching function for call to 'X::X(X)'
    }

The second step of a copy initialization (see 8.5/16/6/2) is a direct-initialization where explicit constructors shall be considered as candidate functions.
Comment 1 Jonathan Wakely 2012-09-07 23:10:27 UTC
Works fine with 4.6.3, 4.7.2 20120716 (prerelease) and 
4.8.0 20120716 (experimental)

As requested when submitting the bug, please provide the information requested at http://gcc.gnu.org/bugs/ including the command line and output of gcc -v
Comment 2 Nikolka 2012-09-08 12:36:29 UTC
(In reply to comment #1)
> Works fine with 4.6.3, 4.7.2 20120716 (prerelease) and 
> 4.8.0 20120716 (experimental)
> 
> As requested when submitting the bug, please provide the information requested
> at http://gcc.gnu.org/bugs/ including the command line and output of gcc -v

1) Version info:

Target: i686-pc-mingw32
Configured with: ../src/configure --prefix=/c/temp/gcc/dest --with-gmp=/c/temp/gcc/gmp --with-mpfr=/c/temp/gcc/mpfr --with-mpc=/c/temp/gcc/mpc --enable-languages=c,c++ --with-arch=i686 --with-tune=generic --disable-libstdcxx-pch --disable-nls --disable-shared --disable-sjlj-exceptions --disable-win32-registry --enable-checking=release --enable-lto
Thread model: win32
gcc version 4.7.1 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-std=c++11' '-mtune=generic' '-march=i686'
 c:/common/c++/mingw/mingw-gcc-4.7.1/bin/../libexec/gcc/i686-pc-mingw32/4.7.1/cc1plus.exe -quiet -v -iprefix ..\bin\../lib/gcc/i686-pc-mingw32/4.7.1/ test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=i686 -auxbase test -std=c++11 -version -o
GNU C++ (GCC) version 4.7.1 (i686-pc-mingw32)
	compiled by GNU C version 4.7.1, GMP version 5.0.5, MPFR version 3.1.0-p10, MPC version 0.9

Diagnostic message:

test.cpp: In function 'int main()':
test.cpp:9:15: error: no matching function for call to 'X::X(X)'
test.cpp:9:15: note: candidate is:
test.cpp:3:9: note: X::X(int)
test.cpp:3:9: note:   no known conversion for argument 1 from 'X' to 'int'

2) Version info:

Target: i686-pc-linux-gnu
Configured with: .../configure --prefix=.../target --enable-languages=c,c++
Thread model: posix
gcc version 4.8.0 20120826 (experimental) (GCC)

Diagnostic message:

test.cpp: In function ‘int main()’:
test.cpp:9:15: error: no matching function for call to ‘X::X(X)’
         X x = 1; // error: no matching function for call to 'X::X(X)'
               ^
test.cpp:9:15: note: candidate is:
test.cpp:3:9: note: X::X(int)
         X(int) {}
         ^
test.cpp:3:9: note:   no known conversion for argument 1 from ‘X’ to ‘int’
Comment 3 Nikolka 2012-09-08 13:25:20 UTC
In both cases (for g++ v4.7.1 and v4.8.0) the only compiler option was -std=c++11. Nothing magical.
Comment 4 Jonathan Wakely 2012-09-09 00:29:45 UTC
Thanks, it would be magical if people could guess what options you used, that's why the bug reporting guidelines ask you to provide that information!

I must have tested the wrong testcase earlier, I can confirm this fails to compile.
Comment 5 Jonathan Wakely 2014-10-27 09:57:45 UTC
*** Bug 63654 has been marked as a duplicate of this bug. ***
Comment 6 Paolo Carlini 2015-07-10 18:36:35 UTC
Mine.
Comment 7 paolo@gcc.gnu.org 2015-07-10 22:57:07 UTC
Author: paolo
Date: Fri Jul 10 22:56:35 2015
New Revision: 225705

URL: https://gcc.gnu.org/viewcvs?rev=225705&root=gcc&view=rev
Log:
/cp
2015-07-10  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/54521
	* call.c (convert_like_real): Do not set LOOKUP_ONLYCONVERTING for
	the second step of copy-initialization.

/testsuite
2015-07-10  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/54521
	* g++.dg/init/explicit3.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/init/explicit3.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Paolo Carlini 2015-07-10 22:58:27 UTC
Fixed for 6.0.