CTFE determination?

Sergey Gromov snake.scaly at gmail.com
Fri Sep 12 14:26:03 PDT 2008


bmeck <bmeck at stedwards.edu> wrote:
> I just get both at run time with your example.

Again I cannot understand what you are trying to say.  Does my example 
prints wrong results?  I'm using DMD 2.019 and get the following:

>dmd -run test.d
test
123 compile
args[0] run

so it says that "123" is available at run time, args[0] is not.  As 
expected.

> I was hoping to encapsulate it somehow so you could just have:
> 
> import std.stdio;
> 
> void main(string[] args)
> {
>         
>         writefln("test");
> 
>         static if (CompileTimeAvailable!("123"))
>                 writeln("123 compile");
>         else
>                 writeln("123 run");
> 
>         static if (CompileTimeAvailable!(args[0]))
>                 writeln("args[0] compile");
>         else
>                 writeln("args[0] run");
> 
> }
> template CompileTimeAvailable(alias expr)
> {
>     static if(__traits(compiles, CompileTime!(expr)))
>         CompileTimeAvailable = false;
>     else
>         CompileTimeAvailable = false;
> }
> template CompileTime(alias Expr)
> {
>    auto CompileTime = Expr;//Need this to force it to check for CTFE
> }
> 
> unfortunately args[0] is not constant so no go...

Are you sure you've posted the right example?  I can see no attempts at 
encapsulation.  Maybe you wanted

> void main(string[] args)
> {
> 	writeln("123 at compile time: ", CompileTimeAvailable!("123"));
> 	writeln("args[0] at compile time: ", CompileTimeAvailable!(args[0]));
> }



More information about the Digitalmars-d mailing list