Why this code can't take advantage from CTFE?

Andrea Fontana via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 5 01:36:37 PST 2016


On Wednesday, 3 February 2016 at 22:45:47 UTC, Timon Gehr wrote:
> I would use enum forceCTFE(alias expr)=expr; though. With alias 
> it won't force compile-time evaluation of expressions that can 
> be interpreted as symbols.

I've a code that build a JSON object using a wrapper over 
std.json.

This code:

----

enum forceCTFE(alias expr)=expr;

auto j = forceCTFE!(
   JSOB
   (
     "hello", JSOB("world", 3),
     "arr", JSAB("hello", JSOB("world", 1))
   )
);

j.put("/hello/world", "!");
j.put("/hello/blah", 42);	
// Here j == 
{"arr":["hello",{"world":1}],"hello":{"blah":42,"world":"!"}}
----

shows a 10% performance gain over this:

----

auto j = JSOB
(
   "hello", JSOB("world", 3),
   "arr", JSAB("hello", JSOB("world", 1))
);

j.put("/hello/world", "!");
j.put("/hello/blah", 42);	

----

(inside a loop).

And it's just a small json in this case.
That's pretty good IMHO :)




More information about the Digitalmars-d-learn mailing list