Battle-plan for CTFE

ag0aep6g via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Sep 1 15:31:37 PDT 2016


On 09/01/2016 11:51 PM, Rory McGuire via Digitalmars-d-announce wrote:
> Yeah, I'm using an enum to force the evaluation during CT,

I had missed that detail. Still, the error seems to be independent of 
any CTFE that's going on.

[...]
> Here is another example:
>
> void main() {
> enum ret = ctfefunc();
> mixin(ret);
> }
>
>
> string ctfefunc() {
> static if (!assertCTFE!true) {
> throw new Exception("Only during ctfe please...");
> }
> return `import std.stdio; writeln("ctfe generated this");`;
> }
>
> template assertCTFE(bool b) {
> enum assertCTFE = __traits(compiles, _checkCTFE1());
> }
> void _checkCTFE1() {
> static if (__ctfe) {
>
> }
> }

It's not clear to me what this example is supposed to show. In 
particular, how does it relate to catching CTFE errors?

Obviously, `static if (__ctfe)` doesn't compile right now, so 
_checkCTFE1 must be rejected regardless of anything else. And with 
_checkCTFE1, the whole program goes down.

Your code looks like it's supposed to throw an exception when ctfefunc 
is called during normal run-time instead of CTFE. That can be done:

----
string ctfefunc()
{
     if (!__ctfe) throw new Exception("Only during ctfe please...");
     return `import std.stdio; writeln("ctfe generated this");`;
}
----

But I feel like that's missing the point of the example.


More information about the Digitalmars-d-announce mailing list