Re: rvalue references: easy to get UB?
on Sun Dec 14 2008, derek-AT-antiquark.com wrote:
On Dec 13, 4:17 pm, David Abrahams <d...@boostpro.com> wrote:
Why not just do that for all temporaries bound to references, while
we're at it? Hmm, it would have to be more than static, though, since
functions can be re-entered. Let's just allocate dynamic memory for all
temporaries, and leak it. Heck, let's do that for all temporaries. Or
all variables. Then, because we'll run out of memory eventually, we can
add garbage collection and no references or pointers will ever dangle.
Why not, Java does it! (hah!)
So would you say that rvalue references are more of a technical
extension that isn't really meant for "everyday" programming?
No; I think everyone who writes classes will want to define move ctor
and move assignment.
For example, with normal references, I use them all the time and don't
worry about UB because the languages places many restrictions on them.
My point is that the language places all the same lifetime restrictions
on regular references, so you have no more reason to fear dangling
rvalue refs than you do to fear dangling references-to-const, since
they'll both bind liberally to temporaries.
X&& a = some_rvalue_expression; // lifetime of a extends to end-of-block
X const& b = some_rvalue_expression; // ditto for b
Would rvalue references be (in a way) similar to using pointers, in
that you have to make sure you're cognizant of the lifetime of rvalue
reference variables?
Yeah, just like any other reference.
--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]