https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81597
Bug ID: 81597 Summary: returns link to temporary value Product: gcc Version: 4.8.5 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lisp2d at rambler dot ru Target Milestone: --- Functions returning C && , C const && , C const & from temporary object will ALWAYS with wrong result. Warning is good but ... To do : prevent return rvalue from those functions. example : //> g++ -std=c++11 returntemp.cpp -o returntemp #include <iostream> int && f ( ) { return 1 ; } int && g ( ) { return 2 ; } int const && ft ( ) { return 7 ; } int const && gt ( ) { return 8 ; } int const & fc ( ) { return 5 ; } int const & gc ( ) { return 6 ; } int fok ( ) { return 3 ; } int gok ( ) { return 4 ; } int main ( int , char * * ) { std :: cout << "int &&" << std :: endl ; { int && x ( f ( ) ) ; std :: cout << "x=" << x << std :: endl ; int && y ( g ( ) ) ; std :: cout << "y=" << y << std :: endl ; std :: cout << "x=" << x << std :: endl ; } std :: cout << "---" << std :: endl ; std :: cout << "int const &&" << std :: endl ; { int const && x ( ft ( ) ) ; std :: cout << "x=" << x << std :: endl ; int const && y ( gt ( ) ) ; std :: cout << "y=" << y << std :: endl ; std :: cout << "x=" << x << std :: endl ; } std :: cout << "---" << std :: endl ; std :: cout << "int const &" << std :: endl ; { int const & x ( fc ( ) ) ; std :: cout << "x=" << x << std :: endl ; int const & y ( gc ( ) ) ; std :: cout << "y=" << y << std :: endl ; std :: cout << "x=" << x << std :: endl ; } std :: cout << "---" << std :: endl ; std :: cout << "int" << std :: endl ; { int && x ( fok ( ) ) ; std :: cout << "x=" << x << std :: endl ; int && y ( gok ( ) ) ; std :: cout << "y=" << y << std :: endl ; std :: cout << "x=" << x << std :: endl ; } } result : int && x=1 y=2 x=32621 --- int const && x=7 y=8 x=32621 --- int const & x=5 y=6 x=32621 --- int x=3 y=4 x=3