www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Detect runtime vs ctfe?

reply "Nick Sabalausky" <a a.a> writes:
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
    }
}
Nov 28 2009
next sibling parent Robert Clipsham <robert octarineparrot.com> writes:
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() ); } ----
Nov 28 2009
prev sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
Nov 29 2009