This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/63654] New: An explicit copy constructor should be used in the second step of a class copy-initialization.


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

            Bug ID: 63654
           Summary: An explicit copy constructor should be used in the
                    second step of a class copy-initialization.
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kariya_mitsuru at hotmail dot com

Created attachment 33815
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33815&action=edit
gcc -v

The sample code below should be compiled successfully but it causes a
compilation error by gcc.

======== sample code =======
struct S {
    explicit S(const S&) {}
    S(int) {}
};

int main()
{
    S s = 1;
}
============================
===================== compiler output ====================================
prog.cc: In function 'int main()':
prog.cc:8:11: error: no matching function for call to 'S::S(S)'
    S s = 1;
          ^
prog.cc:3:5: note: candidate: S::S(int)
    S(int) {}
    ^
prog.cc:3:5: note:   no known conversion for argument 1 from 'S' to 'int'
prog.cc:3:5: note:   after user-defined conversion: S::S(int)
===================== compiler output ====================================
cf. http://melpon.org/wandbox/permlink/fA27PoaI9y9q2Xz6

C++ standard [dcl.init]/p.17.6.2 says that

... The result of the call (which is the temporary for the constructor case) is
then used to direct-initialize, according to the rules above, the object that
is the destination of the copy-initialization. ...

I think that the variable "s" should be *direct-initialize* from the result of
the call "S(int)", so the explicit copy constructor "explicit S(const S&)"
should be used.
(at least if the option "-pedantic-errors" is specified)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]