Bug 40682 - [C++0x] Require direct binding of short-lived references to rvalues
Summary: [C++0x] Require direct binding of short-lived references to rvalues
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.5.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-08 11:04 UTC by Dragan Milenkovic
Modified: 2009-10-07 19:45 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dragan Milenkovic 2009-07-08 11:04:46 UTC
In a recent discussion on comp.std.c++
"rvalue references returned from a function",
as pointed by Niels Dekker and Micael Dark,
a defect report:

  http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#391

was accepted into C++0x WP. This has a significant impact on GCC.

The current behavior also creates a problem with the following:

  struct R {
    R(const R &);
  };

  R && func();

  int main() {
    R const & r1 = func();
    R &&      r2 = func();
  }

Both r1 and r2 will be bound to a temporary which will be
created by copying (or if available - moving) the returned
rvalue. This is invalid according to the mentioned changes.
Comment 1 Paolo Carlini 2009-07-08 11:41:51 UTC
Note, in general work on C++0x features doesn't really belong to Bugzilla, unless existing code crashes on new testcases, things like that. You understand that in general the situation would otherwise quickly explode. About this specific issue, I suspect we even have something already open. Maybe Jon can help...
Comment 2 Dragan Milenkovic 2009-07-08 12:38:23 UTC
Although this is a feature request in the context that the old behavior
was correctly implemented and it will be different in C++0x, it still presents
a bug in the current C++0x implementation. It creates copies of
objects that are returned by a rvalue reference, which would be wrong.
I wasn't _really_ trying to request a new functionality, although
I understand your concern.

However, IMHO in this particular case we are talking about one of
the most fundamental features (binding a reference). This change
could also expose possible bugs in the (user and library) code that
relies on creation of a temporary, which even in C++03 is not
considered a portable code. Don't tell me you'd rather deal with
the issue after all the other C++0x stuff gets implemented...

Anyway, my motive was to help the development of GCC. Personally,
I didn't and will not rely on either of the two behaviors, but
will try to write code that works in both situations.
Comment 3 Paolo Carlini 2009-07-08 12:47:56 UTC
To be clear, I'm not telling you anything specific about the development process. Actually, that's exactly the point, this is ongoing development of experimental features, no guarantees, no guarantees of perfect adherence to the last public draft, etc.
Comment 4 Jason Merrill 2009-10-07 19:45:58 UTC
Neither 4.4.1 nor 4.5 try to call the copy constructor in your example; I believe I fixed this issue in 4.4.