digitalmars.D.learn - Detect runtime vs ctfe?
- Nick Sabalausky (16/16) Nov 28 2009 Is there an idiom, preferably D1, to detect whether or not the code is
- Robert Clipsham (23/43) Nov 28 2009 Not currently, but you can use the following hack, which exploits a bug
- Lars T. Kyllingstad (4/22) Nov 29 2009 Don posted one to the NG once, but I'm not sure if it works with D1:
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
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
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









Robert Clipsham <robert octarineparrot.com> 