Comment #0 by snarwin+bugzilla — 2018-12-01T03:21:09Z
Example program:
--- test.d
struct A
{
static if (__traits(compiles, hasFoo!A)) {
void foo() {}
}
}
enum hasFoo(T) = __traits(hasMember, T, "foo");
void main()
{
import std.stdio;
writeln(hasFoo!A); // false (should be true)
writeln(__traits(hasMember, A, "foo")); // true
}
---
Output:
$ dmd test.d && ./test
false
true
When `hasFoo!A` is instantiated in the `__traits(compiles)` expression, it evaluates to false, because the body of the `static if` statement has not been compiled yet. The same instantiation is then re-used in `main`, even though it is no longer semantically correct.
Possibly related to issue 14803.
Comment #1 by robert.schadek — 2024-12-13T19:01:30Z