Detect runtime vs ctfe?
Robert Clipsham
robert at octarineparrot.com
Sat Nov 28 15:37:59 PST 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() );
}
----
More information about the Digitalmars-d-learn
mailing list