On Wednesday, 4 October 2023 at 01:46:42 UTC, Joel wrote:
> I think the if without static is still static, since it's part of the function name part, or so (outside of the curly bracket scope).
You can't have regular if-statements inside templates. Regular if-statements are checked at runtime but templates only exist at compile-time.
The way to write a template constraint—which is what you want, and uses the if
keyword—is thus:
struct List(T)
if(__traits(isIntegral, T)){
auto addUp()
//(Add numbers)
}
}
Notice that the curly brace for the template comes after the template constraint, and that there are only 2 curly braces rather than the 3 from your example.
If the statement in the template constraint (__traits(isIntegral, T)
) evaluates false at compile-time, then you will get a compiler error saying that the instantiation doesn't match the template constraints.