Determine if CTFE or RT

arturg var.spool.mail700 at gmail.com
Sun Jun 24 20:03:19 UTC 2018


On Sunday, 24 June 2018 at 19:10:36 UTC, Mr.Bingo wrote:
> On Sunday, 24 June 2018 at 18:21:09 UTC, rjframe wrote:
>> On Sun, 24 Jun 2018 14:43:09 +0000, Mr.Bingo wrote:
>>
>>> let is(CTFE == x) mean that x is a compile time constant. 
>>> CTFE(x)
>>> converts a x to this compile time constant. Passing any 
>>> compile time
>>> constant essentially turns the variable in to a compile time
>>> constant(effectively turns it in to a template with template 
>>> parameter)
>>> 
>>
>> You can use __ctfe:
>>
>> static if (__ctfe) {
>>     // compile-time execution
>> } else {
>>     // run-time execution
>> }
>
> This does not work:
>
>
> import std.stdio;
>
> auto foo(int i)
> {
> 	if (__ctfe)
> 	{
> 		return 1;
> 	} else {
> 		return 2;
> 	}
> }
>
> void main()
> {
> 	writeln(foo(3));
> }
>
>
> should print 1 but prints 2.

you have to call foo with ctfe
enum n = foo(3);
writeln(n);


More information about the Digitalmars-d-learn mailing list