Jens Mueller wrote: > Jonathan M Davis wrote: > > On Friday, September 21, 2012 00:11:51 Jens Mueller wrote: > > > I thought foo is interpreted at compile time. > > > There seems to be a subtle difference I'm not getting. > > > Because you can do the factorial using CTFE even though you have > > > recursion. I.e. there you have a call to the function itself. I.e. it > > > can be compiled because you just insert a call to the function. But for > > > a template you cannot issue something like call for instantiation. > > > Have to think more about it. But your answer helps a lot. Pushes me in > > > the right direction. > > > > Okay. Straight up recursion works. So, with this code > > > > int func(int value) > > { > > if(value < 10) > > return func(value + 1); > > return value; > > } > > > > enum var = func(5); > > > > var would be 10. The problem is that you're trying to pass the result of a > > recursive call as a template argument. As far as a function's behavior > > goes, > > it's identical regardless of whether it's run at compile time or runtime > > (save > > that __ctfe is true at compile time but not runtime). To quote the docs: > > > > ------ > > Any functions that execute at compile time must also be executable at > > run time. The compile time evaluation of a function does the > > equivalent > > of running the function at run time. This means that the semantics of a > > function cannot depend on compile time values of the function. For ex > > ample: > > > > int foo(char[] s) { > > return mixin(s); > > } > > > > const int x = foo("1"); > > > > is illegal, because the runtime code for foo() cannot be generated. > > A > > function template would be the appropriate method to implement this > > sort of thing. > > ------ > > Is it also illegal to do > > int foo(char[] s) { > if (__ctfe) > return mixin(s); > else > return ""; // or assert(false) > } > > ? > > Because this is executable at run time.
Just read the docs again. And __ctfe is used to exclude running code at runtime. It seems it really is a variable that is false at runtime. I thought it more a value known at compile time. But then you would write static if anyway. Somehow I find these restrictions unnecessary. I believe they can be solved. Then you could combine CTFE with all other compile-time mechanisms. But this needs more time to think about. Currently I will try to work around this. Let's see ... Jens