Thread overview
Detect runtime vs ctfe?
Nov 28, 2009
Nick Sabalausky
Nov 28, 2009
Robert Clipsham
November 28, 2009
Is there an idiom, preferably D1, to detect whether or not the code is currently executing as a ctfe?

Ie:

void foo()
{
    (static?) if( ???? )
    {
        // Run-time code here
        // that does stuff the CTFE engine chokes on
    }
    else
    {
        // CTFE code here
        // that might not be ideal, but at least works as CTFE
    }
}


November 28, 2009
Nick Sabalausky wrote:
> Is there an idiom, preferably D1, to detect whether or not the code is currently executing as a ctfe?
> 
> Ie:
> 
> void foo()
> {
>     (static?) if( ???? )
>     {
>         // Run-time code here
>         // that does stuff the CTFE engine chokes on
>     }
>     else
>     {
>         // CTFE code here
>         // that might not be ideal, but at least works as CTFE
>     }
> }
> 
> 

Not currently, but you can use the following hack, which exploits a bug with CTFE (if the bug gets fixed then the code below won't work):

----
import tango.io.Stdout;

bool isCtfe()
{
        void test( char[] str )
        {
                str[0] = 'b';
        }
        char[] a = "foo".dup;
        test(a);
        if( a == "boo" )
                return false;
        return true;
}

const ctfe = isCtfe();

void main()
{
        Stdout.formatln( "CTFE: {}; Runtime: {};", ctfe, isCtfe() );
}
----
November 29, 2009
Nick Sabalausky wrote:
> Is there an idiom, preferably D1, to detect whether or not the code is currently executing as a ctfe?
> 
> Ie:
> 
> void foo()
> {
>     (static?) if( ???? )
>     {
>         // Run-time code here
>         // that does stuff the CTFE engine chokes on
>     }
>     else
>     {
>         // CTFE code here
>         // that might not be ideal, but at least works as CTFE
>     }
> }

Don posted one to the NG once, but I'm not sure if it works with D1:

http://www.digitalmars.com/d/archives/digitalmars/D/Going_from_CTFE-land_to_Template-land_101395.html#N101414

-Lars