Compile-time optimization

monarch_dodra monarchdodra at gmail.com
Wed Jul 24 11:20:48 PDT 2013


On Wednesday, 24 July 2013 at 17:10:01 UTC, H. S. Teoh wrote:
> On Wed, Jul 24, 2013 at 09:22:14AM -0700, Ali Çehreli wrote:
>> On 07/24/2013 02:56 AM, monarch_dodra wrote:
>> 
>> > On Tuesday, 23 July 2013 at 17:41:01 UTC, Ali Çehreli wrote:
>> 
>> >> Are you aware of version(ctfe)?
>> >
>> > Hum... really? I don't think that exists.
>> 
>> You are right. I must be remembering old proposals about it.
> [...]
>
> There's if(__ctfe), but unfortunately (IIRC) it doesn't work 
> with static
> if, because CTFE runs after static ifs have been processed. So 
> it might
> not be enough if you have code that has parts that can't run 
> under CTFE.
> Those parts will taint all parent functions up the call tree as
> non-CTFEable, so once you have them you're out of luck.
>
> T

That's not true either. The code will compile, and CTFE will only 
complain if it *runs* into code that can't be executed 
CTFE-style. For example:

//----
import std.stdio;
import core.stdc.string;

int foo()
{
     if(__ctfe)
     {
         return 1;
     }
     else
     {
         int i = void;
         memset(&i, 0, 4);
         return i;
     }
}

void main(string[] args)
{
     enum a = foo();
     auto b = foo();
     writeln(a, " ", b);
}
//----

This compiles and runs fine, even though memset is not ctfe-able.


More information about the Digitalmars-d mailing list