Enum and CTFE function call

Simen Kjærås simen.kjaras at gmail.com
Tue Aug 14 10:28:49 UTC 2018


On Tuesday, 14 August 2018 at 09:17:41 UTC, ixid wrote:
> On Tuesday, 14 August 2018 at 09:12:30 UTC, ixid wrote:
>> This will not compile as it says n is not known at compile 
>> time...
>
> This does work if 'value' is changed to immutable and fun to 
> accept it. So it still seems like a missed opportunity as enum 
> shouldn't be able to change either.

If 'value' is a global immutable, its value is known at 
compile-time, and is equivalent to an enum except you can get its 
address. If it's passed in a compile-time-compatible way 
(generally, as a template parameter), it can thus be used as a 
compile-time constant.

Now, as for your initial post: CTFE is run-time code, run at 
compile-time. In other words, for a function to be CTFE-able, it 
must be possible to run at run-time, and any compile-time 
arguments must be passed in a compile-time-compatible way. Your 
function 'fun' will not compile, because for it to run at 
run-time, 'n' will need to be known.

H. S. Teoh has a great article on the wiki explaining the 
different stages of compilation in D, which I think would be a 
great help in your understanding of the differences:

https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time

--
   Simen


More information about the Digitalmars-d-learn mailing list