Limitations of __traits(compiles, {})

Per Nordlöw per.nordlow at gmail.com
Thu Jul 23 15:00:01 UTC 2020


On Thursday, 23 July 2020 at 13:59:39 UTC, Adam D. Ruppe wrote:
> If the code is in quotes - as a string - then the parser 
> doesn't look at it until it needs to. So you might be able to 
> detect using mixin.
>
> __traits(compiles, mixin(" your code here"))

Thanks.

Solved via mixins as:


@safe pure nothrow @nogc:

private enum hasAutoRefForeach = __traits(compiles, () {
         mixin(`void f() { int[2] _ = [1, 2]; foreach (const auto 
ref e; _) {} }`);
     });

private enum hasRefForeach = __traits(compiles, {
         mixin(`void f() { int[2] _ = [1, 2]; foreach (const ref 
e; _) {} }`);
     });

///
unittest
{
     static assert(hasRefForeach);
     static assert(!hasAutoRefForeach);
}



More information about the Digitalmars-d mailing list