bug in compiles?

Alex AJ at gmail.com
Thu Apr 11 18:13:48 UTC 2019


The following code works when I comment out the static if

//static if (__traits(compiles, __traits(getAttributes, T)))
    static foreach(a;  __traits(getAttributes, T)) Attributes ~=


There seems to be absolutely no reason why this code would fail 
with the static if but pass without it but in the first case I 
get no attributes because the __traits compiles fails.



__traits(compiles, __traits(getAttributes, T))

vs

__traits(getAttributes, T)

How could it not compile in the first case and yet work in the 
foreach?

T is a local variable name passed to this code, which does 
generally return an
error when used in certain ways:


int x;

Code!x; // Basically the code at the top

if I try to do too much with x I get `cannot use local `x` as a 
parameter to a non-global template`. Usually when I try to pass T 
to another template I get this error.


But some things work. But I can't for the life of me understand 
why compiles is failing but the foreach loop is working.


The problem is that I am using compiles quite often and either 
I'm misusing it or it's got some bugs in it that worry me because 
maybe they are silently failing?

Note that I get no errors in my code and I do get the attributes 
so it is technically working.

It would be really nice to know why the compiles is failing, e.g.,

pragma(compilesmsg, __traits(getAttributes, T)) spits out the 
error.







More information about the Digitalmars-d-learn mailing list