Worlds of CTFEs & templates
bearophile
bearophileHUGS at lycos.com
Sat Mar 13 05:19:10 PST 2010
Currently this program doesn't compile:
template Foo(int x) {
static if (x)
enum int Foo = 1;
else
enum int Foo = 0;
}
int bar() {
if (__ctfe) {
for (int i; i < 1; i++)
int r = Foo!(i);
}
return 0;
}
enum int r = bar();
void main() {}
I think it doesn't compile because CTFE functions must be able to run both at compile-time and run-time, so it can't call a template that uses a "run-time" variable, even if it's used only by a CTFE function that knows the value at compile-time :-)
I think that if(__ctfe){ doesn't improve the situation because that's another run-time variable.
But being able to use templates from CTFE functions is very useful, otherwise the worlds of CTFE and templates are too much separated (but I think a template can call a CTFE function), and the compiler knows that the code inside the if(__ctfe){ will never run at run-time.
So can the compiler be modified to allow templates inside the scope of if(__ctfe){...} ?
Bye,
bearophile
More information about the Digitalmars-d
mailing list