The C++ customary board (ISO 14882) simply met earlier this month to ratify the ultimate draft of the C++ 26 customary. You may consider the ultimate draft like a characteristic freeze as a product strikes from beta to launch candidate and we now have to attend for compiler distributors to implement the brand new requirements. A number of thrilling new options simply obtained added to the usual together with contract programming, asynchronous sender/receiver and maybe most significantly the brand new static reflection capabilities.
The brand new reflection particulars as described by Herb Sutter:
I’m optimistic that for a few years to come back we’ll be trying again at at the moment, the day reflection first was adopted for normal C++, as a pivotal date within the language’s historical past. Reflection will basically enhance the best way we write C++ code, develop the expressiveness of the language greater than we’ve seen in at the least 20 years, and result in main simplifications in real-world C++ toolchains and environments. Even with the primary partial reflection functionality we now have at the moment, we’ll already be capable to replicate on C++ varieties and use that data plus plain previous std::cout to generate arbitrary extra C++ supply code that’s based mostly on that data and that we are able to compile and hyperlink into the identical program because it’s being constructed. (Sooner or later we’ll additionally get token injection to generate C++ supply proper throughout the similar supply file.) However we are able to generate something: Arbitrary binary metadata, reminiscent of a .WINMD file. Arbitrary code in different languages, reminiscent of Python or JS bindings mechanically generated to wrap C++ varieties. All in moveable customary C++.
This can be a Large Bushy Deal. Look, everybody is aware of I’m biased towards saying good issues about C++, however I don’t go in for hyperbole and I’ve by no means stated something like this earlier than. In the present day is legit distinctive: Reflection is extra transformational than any 10 different main options we’ve ever voted into the usual mixed, and it’ll dominate the following decade (and extra) of C++ as we full the characteristic with extra capabilities (simply as we added to constexpr over time to fill that out) and discover ways to use it in our applications and construct environments.
You may see an instance of the brand new reflection capabilities from InfoQ showcasing turning an enum into string values at compile time:
enum Colour { crimson, inexperienced, blue }; static_assert(enum_to_string(Colour::crimson) == "crimson"); templatetemplate requires std::is_enum_v constexpr std::string enum_to_string(E worth) { template for (constexpr auto e : std::meta::members_of(^E)) { if (worth == (:e:)) { return std::string(std::meta::name_of(e)); } } return " "; }
Key Hyperlinks
You may study extra concerning the new options coming in C++ 26 within the video beneath.