Going from CTFE-land to Template-land

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 17 05:28:58 PST 2009


On Tue, 17 Nov 2009 08:14:55 -0500, Bill Baxter <wbaxter at gmail.com> wrote:

> Currently this doesn't work, because the CTFE function doesn't "know"
> that it's running compile-time:
>
> int templ_incr(int x)() {
>     return x+1;
> }
>
> int ctfe_incr(int x) {
>     return templ_incr!(x);
> }
>
> Seems common to write a function that you know is only intended to be
> used compile-time.
> But it can't compile because the compiler doesn't know you only plan
> to call it at compile-time.
>
> Is something version(__ctfe) might help with?  E.g.
> version(__ctfe) {
>     // only allow cfte_incr to be called at compile-time so it can use  
> templates
>     int ctfe_incr(int x) {
>         return templ_incr!(x);
>     }
> }
>
> Or is there something more fundamental preventing CTFE funcs from
> instantiating templates?

I think it may be a valid point, but it also may be a case of factoring.   
Do you have a real example?  In the one you posted, you can trivially  
rewrite

ctfe_incr(x);

as

templ_incr!(x)(); // not sure if parens are optional there...

or trivially rewrite ctfe_incr as:

int ctfe_incr(int x) {
   return x + 1;
}

A real example would go a long way in furthering the cause...

-Steve



More information about the Digitalmars-d mailing list