CTFE vs. traditional metaprogramming

language_fan foo at bar.com.invalid
Mon Oct 12 04:48:31 PDT 2009


Mon, 12 Oct 2009 12:07:19 +0200, Don thusly wrote:

> language_fan wrote:
>> Sat, 10 Oct 2009 10:30:31 +0200, Don thusly wrote:
>> 
>>> The more fundamental problem is that you can't instantiate a template
>>> from inside CTFE. IE, you can cross from the "compile-time world" to
>>> the "runtime world" only once -- you can never get back.
>> 
>> That's not exactly true. Also both templates and CTFE are compile time
>> features. You can compute a value with CTFE in the "value world" and
>> lift the result to the "type world" with a template.
> 
> Yes, but the problem is that variables inside a CTFE function, even
> though they are known at compile-time, are not permitted to be used as
> template value parameters. For example:
> 
> template A(int X) { enum int A = B(X)+1; } // OK, template can call CTFE
> 
> int B(int X) { return A!(X) + 1; }
>      // Not OK, CTFE cannot call template.

As far as I can tell there is no reason why you cannot call templates 
from a CTFE code. Your code above has two problems: a) it never 
terminates b) due to some lookup problem the compiler gets confused, this 
has nothing to do with CTFE not being able to call templates - for 
instance this works:

> template A(int X) { enum int A = 2+1; }
> 
> int B(int X) { return A!(X) + 1; }



More information about the Digitalmars-d mailing list