public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/46004] New: [C++0x] template constructor used to copy object
@ 2010-10-13 12:01 redi at gcc dot gnu.org
  2010-10-13 13:10 ` [Bug c++/46004] " jason at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2010-10-13 12:01 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: [C++0x] template constructor used to copy object
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: redi@gcc.gnu.org
                CC: jason@gcc.gnu.org


[class.copy]p7 says "A member function template is never instantiated to
perform the copy of a class object to an object of its class type."


struct B
{
    int i;

    template<typename I>
        explicit
        B(I&& ii) : i(ii) { }

    B(const B&) = default;
};

int main()
{
    B b(0);
    B bb(b);
}

cc.cc: In constructor 'B::B(I&&) [with I = B&]':
cc.cc:15:11:   instantiated from here
cc.cc:7:25: error: cannot convert 'B' to 'int' in initialization

Even with an explicitly declared copy constructor the template is still chosen.

This only happens if the template constructor parameter type is an
rvalue-reference, so only affects C++0x mode.

In http://gcc.gnu.org/viewcvs?view=revision&revision=165144 I had to workaround
this behaviour by replacing the template constructor by a pair of overloads,
one for lvalues and one for rvalues:

    explicit B(const int& ii) : i(ii) { }
    explicit B(int&& ii) : i(ii) { }


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

* [Bug c++/46004] [C++0x] template constructor used to copy object
  2010-10-13 12:01 [Bug c++/46004] New: [C++0x] template constructor used to copy object redi at gcc dot gnu.org
@ 2010-10-13 13:10 ` jason at gcc dot gnu.org
  2010-10-13 13:25 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: jason at gcc dot gnu.org @ 2010-10-13 13:10 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Jason Merrill <jason at gcc dot gnu.org> 2010-10-13 13:10:20 UTC ---
That passage is poorly worded, leading to frequent misinterpretation.  What it
means is that a template will never be instantiated to provide the signature
B(B); it can still be instantiated to provide B(B&), as in this example.


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

* [Bug c++/46004] [C++0x] template constructor used to copy object
  2010-10-13 12:01 [Bug c++/46004] New: [C++0x] template constructor used to copy object redi at gcc dot gnu.org
  2010-10-13 13:10 ` [Bug c++/46004] " jason at gcc dot gnu.org
@ 2010-10-13 13:25 ` redi at gcc dot gnu.org
  2011-09-23 13:08 ` redi at gcc dot gnu.org
  2011-10-31 12:21 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2010-10-13 13:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-10-13 13:24:59 UTC ---
Aaaaah! thanks for clearing that up.

So in cases where I do want/need a template ctor with &&-style deduction I'll
use enable_if to prevent it being instantiated to perform a copy.

thanks again


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

* [Bug c++/46004] [C++0x] template constructor used to copy object
  2010-10-13 12:01 [Bug c++/46004] New: [C++0x] template constructor used to copy object redi at gcc dot gnu.org
  2010-10-13 13:10 ` [Bug c++/46004] " jason at gcc dot gnu.org
  2010-10-13 13:25 ` redi at gcc dot gnu.org
@ 2011-09-23 13:08 ` redi at gcc dot gnu.org
  2011-10-31 12:21 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2011-09-23 13:08 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |boostcpp at gmail dot com

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-09-23 12:58:17 UTC ---
*** Bug 45755 has been marked as a duplicate of this bug. ***


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

* [Bug c++/46004] [C++0x] template constructor used to copy object
  2010-10-13 12:01 [Bug c++/46004] New: [C++0x] template constructor used to copy object redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2011-09-23 13:08 ` redi at gcc dot gnu.org
@ 2011-10-31 12:21 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 5+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-10-31 12:21 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jarrydb at cse dot
                   |                            |unsw.edu.au

--- Comment #5 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-10-31 12:19:51 UTC ---
*** Bug 50929 has been marked as a duplicate of this bug. ***


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

end of thread, other threads:[~2011-10-31 12:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-13 12:01 [Bug c++/46004] New: [C++0x] template constructor used to copy object redi at gcc dot gnu.org
2010-10-13 13:10 ` [Bug c++/46004] " jason at gcc dot gnu.org
2010-10-13 13:25 ` redi at gcc dot gnu.org
2011-09-23 13:08 ` redi at gcc dot gnu.org
2011-10-31 12:21 ` paolo.carlini at oracle dot com

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).