how do I tell if something is lvalue?

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 1 12:42:36 PST 2016


On 02/01/16 20:47, Meta via Digitalmars-d-learn wrote:
> On Monday, 1 February 2016 at 18:28:05 UTC, Artur Skawina wrote:
>> On 01/31/16 23:11, Steven Schveighoffer via Digitalmars-d-learn wrote:
>>> Thanks! I was surprised this is not straightforward.
>>
>>    enum isLvalue(alias A) = is(typeof((ref _){}(A)));
> 
> That looks much nicer. It still needs work to properly handle functions with non-empty argument lists.

Then it gets a bit long for a one-liner ;)

   enum isLvalue(A...) = is(typeof((ref _){}(A[0](A[1..$])))) || is(typeof((ref _){}(A[0])));

> Also, can alias parameters take runtime variables? I can't remember.

Yes.

> 
> struct S
> {
>     int w(int n) { return 1; }
      ref int wl(int n) { return x; }
> }
> 
> static assert(isLvalue!(S.w));


   static assert(!isLvalue!(S.w, 1));
   static assert(isLvalue!(S.wl, 1));


artur


More information about the Digitalmars-d-learn mailing list