public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52901] New: invalid rvalue reference
@ 2012-04-07 16:35 dieter.lucking at googlemail dot com
2012-04-08 13:49 ` [Bug c++/52901] " daniel.kruegler at googlemail dot com
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: dieter.lucking at googlemail dot com @ 2012-04-07 16:35 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52901
Bug #: 52901
Summary: invalid rvalue reference
Classification: Unclassified
Product: gcc
Version: 4.6.1
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: dieter.lucking@googlemail.com
Created attachment 27111
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27111
code
A different result - just reordering the cout output.
#include <iostream>
using namespace std;
struct X {
int value;
X() {
cout << "default construction" << std::endl;
value = 0;
}
X(const X& other) {
cout << "copy construction" << std::endl;
value = 1;
}
X(X&& other) {
cout << "move construction" << std::endl;
value = 2;
}
X& operator = (const X& other) {
cout << "assignment" << std::endl;
value = 3;
return *this;
}
};
X&& f() {
X x;
return std::move(x);
}
int main() {
cout << "Hello References [1]" << std::endl;
X x0 = f();
cout << "x0: " << x0.value << std::endl;
X&& x1 = f();
cout << "No copy construction or assignment expected" << std::endl;
cout << "x1: " << x1.value << std::endl;
cout << "Hello References [2]" << std::endl;
X y0 = f();
cout << "y0: " << y0.value << std::endl;
X&& y1 = f();
cout << "y1: " << y1.value << std::endl;
cout << "No copy construction or assignment expected" << std::endl;
return 0;
}
// Seems g++ 4.6.1 is buggy:
// Hello References [1]
// default construction
// move construction
// x0: 2
// default construction
// No copy construction or assignment expected
// x1: 6295648
// Hello References [2]
// default construction
// move construction
// y0: 2
// default construction
// y1: 0
// No copy construction or assignment expected
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/52901] invalid rvalue reference
2012-04-07 16:35 [Bug c++/52901] New: invalid rvalue reference dieter.lucking at googlemail dot com
@ 2012-04-08 13:49 ` daniel.kruegler at googlemail dot com
2012-04-08 14:15 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2012-04-08 13:49 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52901
Daniel Krügler <daniel.kruegler at googlemail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |daniel.kruegler at
| |googlemail dot com
--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2012-04-08 13:48:57 UTC ---
I don't see what this example is supposed to demonstrate. the two lines
X&& x1 = f();
and
X&& y1 = f();
produce references to invalid memory, any access to that is undefined
behaviour. You cannot impose any reliable constraints on the values such
references.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/52901] invalid rvalue reference
2012-04-07 16:35 [Bug c++/52901] New: invalid rvalue reference dieter.lucking at googlemail dot com
2012-04-08 13:49 ` [Bug c++/52901] " daniel.kruegler at googlemail dot com
@ 2012-04-08 14:15 ` redi at gcc dot gnu.org
2012-04-08 15:33 ` marc.glisse at normalesup dot org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2012-04-08 14:15 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52901
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-04-08 14:14:30 UTC ---
This is just a bug in your program, not G++
(In reply to comment #0)
>
> X&& f() {
> X x;
> return std::move(x);
> }
This function is unsafe, it returns a reference to a local variable. You
probably meant it to return X not X&&
It is effectively the same as:
X& f() {
X x;
return x;
}
(except G++ warns about that, because it's simpler)
>
> int main() {
> cout << "Hello References [1]" << std::endl;
> X x0 = f();
> cout << "x0: " << x0.value << std::endl;
> X&& x1 = f();
This reference is bound to a variable that went out of scope when f() returned.
> cout << "No copy construction or assignment expected" << std::endl;
> cout << "x1: " << x1.value << std::endl;
This accesses deallocated memory.
N.B. you don't even need to use std::move, the compiler will automatically
select the move constructor to create the return value here:
X f()
{
X x;
return x;
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/52901] invalid rvalue reference
2012-04-07 16:35 [Bug c++/52901] New: invalid rvalue reference dieter.lucking at googlemail dot com
2012-04-08 13:49 ` [Bug c++/52901] " daniel.kruegler at googlemail dot com
2012-04-08 14:15 ` redi at gcc dot gnu.org
@ 2012-04-08 15:33 ` marc.glisse at normalesup dot org
2012-04-08 18:06 ` redi at gcc dot gnu.org
2012-04-08 18:09 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: marc.glisse at normalesup dot org @ 2012-04-08 15:33 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52901
Marc Glisse <marc.glisse at normalesup dot org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |marc.glisse at normalesup
| |dot org
--- Comment #3 from Marc Glisse <marc.glisse at normalesup dot org> 2012-04-08 15:32:45 UTC ---
(In reply to comment #2)
> > X&& f() {
> > X x;
> > return std::move(x);
> > }
>
> This function is unsafe, it returns a reference to a local variable. You
> probably meant it to return X not X&&
>
> It is effectively the same as:
>
> X& f() {
> X x;
> return x;
> }
>
> (except G++ warns about that, because it's simpler)
Maybe this could be taken as a RFE for a warning with std::move? Many people
learning C++11 are bound to try similar things. g++ warns for
return X();
return static_cast<X&&>(x);
but not
return std::move(x);
I expect the case of std::move to be important enough that if doing a generic
warning is too hard, special-casing std::move could be worth the trouble
(assuming it is easier).
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/52901] invalid rvalue reference
2012-04-07 16:35 [Bug c++/52901] New: invalid rvalue reference dieter.lucking at googlemail dot com
` (2 preceding siblings ...)
2012-04-08 15:33 ` marc.glisse at normalesup dot org
@ 2012-04-08 18:06 ` redi at gcc dot gnu.org
2012-04-08 18:09 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2012-04-08 18:06 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52901
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Severity|major |normal
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-04-08 18:05:49 UTC ---
(In reply to comment #3)
> g++ warns for
>
> return X();
> return static_cast<X&&>(x);
>
> but not
>
> return std::move(x);
Nor for
X& g(X& x) { return x; }
X& f() {
X x;
return g(x);
}
I opened PR 49974 to request a warning there, we don't need another :)
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/52901] invalid rvalue reference
2012-04-07 16:35 [Bug c++/52901] New: invalid rvalue reference dieter.lucking at googlemail dot com
` (3 preceding siblings ...)
2012-04-08 18:06 ` redi at gcc dot gnu.org
@ 2012-04-08 18:09 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2012-04-08 18:09 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52901
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-04-08 18:09:06 UTC ---
See also PR 51066 and PR 51270 and PR 38958 for similar RFEs for better
diagnostics about creating dangling references
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-04-08 18:09 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-07 16:35 [Bug c++/52901] New: invalid rvalue reference dieter.lucking at googlemail dot com
2012-04-08 13:49 ` [Bug c++/52901] " daniel.kruegler at googlemail dot com
2012-04-08 14:15 ` redi at gcc dot gnu.org
2012-04-08 15:33 ` marc.glisse at normalesup dot org
2012-04-08 18:06 ` redi at gcc dot gnu.org
2012-04-08 18:09 ` redi 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).