digitalmars.D.learn - Question about compile-time functions.
- Bauss (2/2) Dec 13 2016 If a function is only called during compile-time will it be
- Nicholas Wilson (10/12) Dec 14 2016 It depends. When linking unused functions can be removed by
- Johan Engelen (14/16) Dec 14 2016 With "available at runtime", I guess you mean "will it be part of
If a function is only called during compile-time will it be available at runtime?
Dec 13 2016
On Wednesday, 14 December 2016 at 07:15:08 UTC, Bauss wrote:If a function is only called during compile-time will it be available at runtime?It depends. When linking unused functions can be removed by -gc-sections, but only when linking an executable. But obviously if you use it at runtime then the linker won't remove it. If you want to ensure that it never gets called at runtime, put a `assert(__ctfe);` at the beginning of the function. There was a discussion some time ago about having an attribute(?) to disable codegen for particular functions (e.g. code for generating string for string mixins), but i'm not sure what became of that.
Dec 14 2016
On Wednesday, 14 December 2016 at 07:15:08 UTC, Bauss wrote:If a function is only called during compile-time will it be available at runtime?With "available at runtime", I guess you mean "will it be part of the object file". In that case: yes. Because even if a function is _never_ called, it will be part of the object file (it must be). For templated functions, the answer is not so easy. Templates that are only instantiated during CTFE are usually also emitted into the object file. But, when importing a templated function from another module, the template instantiation is not emitted to the object file if the compiler deduces that the instantiation is already emitted in the object file result of compiling the imported module (to avoid unnecessary duplicate instantiations across compilation units). -Johan
Dec 14 2016