assert(__ctfe) and betterC

Tim tim.dlang at t-online.de
Wed Jan 27 21:05:36 UTC 2021


On Wednesday, 27 January 2021 at 20:59:24 UTC, Steven 
Schveighoffer wrote:
> CTFE is the primary vehicle used to generate dynamic mixin code 
> based on template and regular parameters. Doing it with 
> templates *only* is possible, but so so painful.
>
> But, the general mechanisms used in CTFE to generate string 
> data (i.e. string concatenation) are not available for betterC 
> runtime code. And CTFE must be compiled to as runtime code 
> before it is interpreted.
>
> I remember something about assert(__ctfe) in your function 
> telling the compiler to not emit the code to the object file. 
> Did that get in? Is there a way we can use that to turn off 
> betterC too?
>
> Otherwise, you get errors when building your CTFE function 
> because "TypeInfo cannot be used with -betterC"
>
> Is there another way I can do this?
>
> -Steve

One way I have sometimes used is an alias to a function literal:

alias generateVar = function(string name) {
     return "int " ~ name ~ ";";
};

mixin(generateVar("i"));

extern(C) int main()
{
     i = 0;
     return i;
}

This works with betterC.


More information about the Digitalmars-d mailing list