auto ref escaping local variable

Nordlöw via Digitalmars-d digitalmars-d at puremagic.com
Tue Jan 24 01:32:34 PST 2017


On Tuesday, 24 January 2017 at 08:49:17 UTC, Ali Çehreli wrote:
> On 01/24/2017 12:47 AM, Ali Çehreli wrote:
>
> > Lvalues are passed by reference and rvalues are copied.
>
> I keep making that mistake! Despite the by-copy syntax, rvalues 
> are moved.
>
> Ali

Further note that you can use conditional compilation with 
`__traits(isRef)` as in

struct S {}

void f()(auto ref const S x)
{
     static if (__traits(isRef, x)) // l-value `x` was passed by 
ref
     {
         // special treatment of const ref x
     }
     else  // r-value `x` was passed by move
     {
         // `x´ can be reused, for instance in a move-return
     }
}

This is actually a very useful feature in some cases.

Used, for instance, in gmp-d announced yesterday here

http://forum.dlang.org/thread/mwkehzkdbwzygngeaonf@forum.dlang.org

for clever expression template optimization possible. AFAIK a 
D-only feature.


More information about the Digitalmars-d mailing list