On Thursday, 11 April 2019 at 19:42:05 UTC, Steven Schveighoffer
wrote:
On 4/11/19 2:13 PM, Alex wrote:
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?
It should. Do you have a larger example? One that can be played
with?
-Steve
import std.stdio, std.traits, std.conv;
struct A(alias T)
{
static void foo()
{
static if (__traits(compiles, __traits(getAttributes, T))) //
Failing for some reason
static foreach(a; __traits(getAttributes, T)) pragma(msg,
to!string(a), "-", typeof(a).stringof);
}
}
void main()
{
@(3) int a;
(A!a).foo();
}
Commenting out the static if allows the foreach to pass and
everything works. The whole point of the static if is to let the
foreach work.
Both are only dependent on
__traits(getAttributes, T)
So if that can't compile then how could the foreach work?
https://run.dlang.io/is/WlXCIZ