struct template constructors

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 22 13:05:46 PDT 2017


On 06/22/2017 12:57 PM, Boris-Barboris wrote:
 > On Thursday, 22 June 2017 at 19:17:13 UTC, Ali Çehreli wrote:
 >> No time to think about the rest of the design but just to get the code
 >> compiled, replace 'ref' with 'auto ref' like so:
 >
 > Ok, looks like this indeed passes rhs by reference, thank you.

To be complete, 'auto ref' passes lvalues by reference and rvalues by 
value, which you can detect with __traits(isRef):

struct S{
}

void foo()(auto ref S s) {
     static if (__traits(isRef, s)) {
         pragma(msg, "lvalue");
     } else {
         pragma(msg, "rvalue");
     }
}

void main() {
     auto s = S();
     foo(s);

     foo(S());
}

Ali



More information about the Digitalmars-d-learn mailing list