Discover C++26's compile-time reflection

https://news.ycombinator.com/rss Hits: 2
Summary

Herb Sutter just announced that the verdict is in: C++26, the next version of C++, will include compile-time reflection. Reflection in programming languages means that you have access the code’s own structure. For example, you can take a class, and enumerate its methods. For example, you could receive a class, check whether it contains a method that returns a string, call this method and get the string. Most programming languages have some form of reflection. For example, the good old Java does have complete reflection support. However, C++ is getting compile-time reflection. It is an important development. I announced a few months ago that thanks to joint work with Francisco Geiman Thiesen, the performance-oriented JSON library simdjson would support compile-time reflection as soon as mainstream compilers support it. This allows you to take your own data structure and convert it to a JSON string without any effort, and at high speed: kid k{12, "John", {"car", "ball"}}; simdjson::to_json(k); // would generate {"age": 12, "name": "John", "toys": ["car", "ball"]} And you can also go back, given a JSON document, you can get back an instance of your custom type: kid k = doc.get<kid>(); The code can be highly optimized and it can be thoroughly tested, in the main library. Removing the need for boilerplate code has multiple benefits. To illustrate the idea further, let me consider the case of object-to-SQL mapping. Suppose you have your own custom type: struct User { int id; std::string name; double balance; private: int secret; // Ignored in SQL generation }; You want to insert an instance of this user into your database. You somehow need to convert it to a string such as INSERT INTO tbl (id, name, balance) VALUES (0, '', 0.000000); How easy can it be? With compile-time reflection, we can make highly efficient and as simple as single function call: generate_sql_insert(u, "tbl"); Of course, the heavy lifting still needs to be done. But it only needs to be done once. What ...

First seen: 2025-06-22 18:56

Last seen: 2025-06-22 19:56