Classes and CTFE

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 10 06:36:18 PST 2016


On Thursday, 10 March 2016 at 13:56:18 UTC, Andrea Fontana wrote:
> I used to think that classes can't be used with CTFE.

Classes have worked normally with CTFE for several years now. You 
don't need to do anything special with them.

> Ex: http://dpaste.dzfl.pl/5879511dff02

This just doesn't do what you think it does:

if (__ctfe) pragma(msg, "compile-time");

That will ALWAYS print the thing because if(__ctfe) is a *run 
time* branch, and pragma(msg) is a compile time thing. The code 
gets compiled, even if __ctfe == false, so it will print anyway.

enum forceCTFE(alias expr)=expr;


That's only one way to do CTFE. Notice the error message:

variable p.forceCTFE!(willnot).forceCTFE : Unable to initialize 
enum with class or pointer to struct. Use static const variable 
instead.



enums don't work in references, so you do a static variable 
instead. Static variables are still CTFE'd.


More information about the Digitalmars-d-learn mailing list