r/cpp • u/User_Deprecated • Jun 15 '26
Parsing JSON at compile time with C++26 static reflection (Daniel Lemire)
https://lemire.me/blog/2026/06/14/parsing-json-at-compile-time-with-c26-static-reflection/
129
Upvotes
69
u/jk_tx Jun 15 '26
So many of the reflection samples seem really contrived to me. Why on earth would you hard code a json string into your source to parse at compile time when you could just directly initialize a constexpr struct and skip the json?
14
u/germandiago Jun 15 '26 edited Jun 15 '26
I predict a great future for C++ as a fast backend language starting in C++26. Reflection is the killer feature, annotations can add routes to web frameworks, serialization is solved...
By the way, it is possible to have something similar to a decorator pattern (like in Python) with C++26?
16
u/PhilosophyMammoth748 Jun 15 '26
Good. After 60 years of complier running, it produce a binary equivalent to "std::cout<<42;"
3
u/zerhud Jun 15 '26
It possible with cpp20: we can parse file and get access like obj.field(string_c<“field”>). But without #embed it seems we can use #include and put raw string delimiters inside file.
2
u/binaryfireball Jun 15 '26
the premise is weird. If its compile time Id rather just have constants.
6
u/_Noreturn Jun 15 '26 edited Jun 15 '26
I really would like to understand the purpose of this. at this point you should just write apython script that parses the json and outputs a header.
You get simpler code and it would compile faster than having to redo all the work in every translation unit
1
u/Antagonin Jul 27 '26
How much stuff could be possible with this? Could you for example define a whole new language that gets transpiled into Cpp at compile time?
0
u/stanley_420_yelnats Jun 15 '26
Surely there's a more lightweight JSON library that supports compile-time parsing? simdjson looks great, but I'm not sure I want all 7MBs of it if I don't plan on parsing JSON at runtime