Const ref and rvalues again...
Malte Skarupke
malteskarupke at web.de
Thu Oct 18 18:26:28 PDT 2012
On Friday, 19 October 2012 at 00:03:49 UTC, Timon Gehr wrote:
>
> Const is different in D and in C++. Relating const and rvalues
> is arbitrary and does not make a lot of sense.
>
> Regarding 'in ref'/'scope ref': What should 'scope' apply to in
>
> void foo(scope ref int* x);
Not sure what you mean with "relating." I'm not making any claims
about there being a relationship between rvalues and constness.
This is about finding a way that you can define a function which
safely accepts lvalues and rvalues without having to make a copy.
If we specify the argument as "ref in", then we can safely pass
for example the number 5 to it. And this would never break
existing code, so that something like swap(5, 4) would never be
possible code.
For the example that you gave you'd be unable to store the
address of x. So doing
int** storage;
void foo(scope ref int * x)
{
storage = &x;
}
would be illegal.
@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.
More information about the Digitalmars-d
mailing list