how do I tell if something is lvalue?

tsbockman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 1 21:07:32 PST 2016


On Monday, 1 February 2016 at 22:32:26 UTC, Steven Schveighoffer 
wrote:
> What I wanted essentially was a template constraint that says 
> "this type has a member named foo, and t.foo is an lvalue"
>
> -Steve

Like this?

template hasLValProperty(T, string property) {
     enum hasLValProperty = __traits(compiles, function(ref T x) {
         void requireLVal(V)(ref V y) { }
         requireLVal(mixin("x." ~ property));
     });
}

struct Foo {
     int a;
}
struct Bar {
     int b() {
         return 0; }
}
struct Baz {
     ref int c() {
         static int _c;
         return _c;
     }
}

void test(T, string property)() {
     import std.stdio;
     write(T.stringof, ".", property, " is ");
     if(!hasLValProperty!(T, property))
         write("NOT ");
     writeln("an lvalue");
}

void main() {
     import std.stdio;
	
     test!(Foo, "a")();
     test!(Foo, "b")();
     test!(Bar, "a")();
     test!(Bar, "b")();
     test!(Baz, "c")();
}

(DPaste: http://dpaste.dzfl.pl/5877cc17ffd2)


More information about the Digitalmars-d-learn mailing list