Consider the code below. The first static assert passes, the second fails:
template Works(T)
{
static if (is(T U == const U))
{
pragma(msg, "is const");
alias Works = U;
}
else
{
alias Works = T;
}
}
template Fails(T)
{
alias Fails = T;
static if (is(T U == const U))
{
pragma(msg, "assigning");
Fails = U;
}
}
static assert(is(Works!(const int) == int));
static assert(is(Fails!(const int) == int));
Comment #1 by destructionator — 2021-05-06T15:43:08Z
I think this is undefined in the language.... at declaration level, there is no real order of operations. So the static if and assign conceptually happen simultaneously (this is why D doesn't need forward declarations).
Obviously the alias assign thing does not follow this rule. But then we need to decide: does the rule change or does alias assign change? And i don't know yet.
Comment #2 by andrei — 2021-05-06T16:19:43Z
@Walter if that's the case, is there a possibility for the compiler to always mark invalid uses as errors?
Comment #3 by robert.schadek — 2024-12-13T19:16:14Z