Determine if CTFE or RT

Mr.Bingo Bingo at Namo.com
Mon Jun 25 05:03:26 UTC 2018


On Sunday, 24 June 2018 at 20:03:19 UTC, arturg wrote:
> 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);

This defeats the whole purpose. The whole point is for the 
compiler to automatically compute foo(3) since it can be 
computed. Now, with your code, there is no way to simplify code 
like

foo(3) + foo(8);



auto foo(int i)
{
    if (__ctfe && i == 3)
    {
	return 1;
    } else {
	return 2;
    }
}


Now, this would precompute foo(3), unbeknownst to the caller of 
foo but then this requires, using your method, to write the code 
like

enum x = foo(3);
x + foo(8);

We would have to know which values foo would return as compile 
time values and what not.

foo(x) + foo(y)

could not work simultaneously for both compile time and run time 
variables.


e.g.,
enum x = 4;
enum y = 4;

foo(x) + foo(y)


would not precompute the values even though x and y are enum's, 
we have to do


enum x = 4;
enum y = 4;
enum xx = foo(x);
enum yy = foo(y);
xx + yy;


and if we changed x or y to a run time variable we'd then have to 
rewrite the expressions since your technique will then fail.

int x = 4;
enum y = 4;
enum xx = foo(x); // Invalid
enum yy = foo(y);
xx + yy;


The compiler should be easily able to figure out that foo(3) can 
be precomputed(ctfe'ed) and do so. It can already do this, as you 
say, by forcing enum on it. Why can't the compiler figure it out 
directly?

The problem here, if you didn't understand, is that one can't get 
foo to be "precomputed" IF it can be done, but IF NOT then it 
will just get the full computation. Because one does not 
necessarily know when and where and how foo will be 
precomputed(or even have completely different behavior for ctfe 
vs rtfe), one can't use two different methods that have the same 
syntax.







More information about the Digitalmars-d-learn mailing list