Const ref and rvalues again...

jerro a at a.com
Thu Oct 18 20:49:53 PDT 2012


> @jerro: the same thing: I'm not trying to fix the problem that 
> you mention. I'm trying to define a function which can safely 
> accept rvalues and lvalues without having to make a copy.

My point was that saving an address of a const ref parameter is 
already unsafe if you call the function with a local variable as 
the parameter. If this behavior seems problematic to you when it 
concerns rvalues, it should seem equally problematic when it 
comes to local variables. It doesn't make sense to make passing 
rvalues as const ref parameters illegal because of this problem, 
when passing local variables causes the same problem and is 
legal. It would only make sense to introduce "in ref" whith an 
intent to solve this problem, if local variables would also have 
to be passed as "in ref" (or some kind of scope ref, not 
necessarily const). But that would break pretty much all code 
that uses ref.

The only case I can think of when passing a local variable as 
const ref is safe, but passing an rvalue wouldn't be, is when the 
called function returns the address of the const parameter (or 
assigns it to some other ref parameter).


More information about the Digitalmars-d mailing list