Re: rvalue references: easy to get UB?
On 11 dic, 09:22, de...@antiquark.com wrote:
Hello All,
I have downloaded a recent version of g++ (4.3.2) which provides some
features from C++0x. I've been trying various things with rvalue
references, to see how they behave and get an intuition of what can be
done with them.
It seems to be easy to obtain a reference to a temporary that no
longer exists. Is this expected behavior (and thus UB), or should the
complier produce an error or warning?
Yes its easier, but if your type has a move constructor you'd better
return by value rather than by reference if you are in doubt
This was explained in depth here:
http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/99ae7ed3b6b4548e/070e8cc916f7a47b
If this is UB, could the problem be alleviated by increasing the
lifespan of "rvalue reference temporaries (RRT's)" to the end of the
block where the RRT was created? (Just a thought.)
Probably not. If the function is not inlined I think its not possible:
// file1.cpp
string&& weird(string&& a, string&& b)
{
return ((std::rand( ) == 1)? a : b);
}
//file2.cpp
string&& c = weird("a", "b"); // which temporary should have its
lifespan increased?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]