26.2 C
New York
Monday, June 30, 2025

C++ 26 Last Draft – GameFromScratch.com


The C++ commonplace board (ISO 14882) simply met earlier this month to ratify the ultimate draft of the C++ 26 commonplace. You possibly can consider the ultimate draft like a function 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 received 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 constructive that for a few years to come back we’ll be trying again at in the present day, the day reflection first was adopted for traditional C++, as a pivotal date within the language’s historical past. Reflection will essentially enhance the way in which we write C++ code, increase the expressiveness of the language greater than we’ve seen in no less than 20 years, and result in main simplifications in real-world C++ toolchains and environments. Even with the primary partial reflection functionality we now have in the present day, we are going to already be capable to mirror on C++ sorts and use that data plus plain previous std::cout to generate arbitrary further C++ supply code that’s based mostly on that data and that we will 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 will generate something: Arbitrary binary metadata, comparable to a .WINMD file. Arbitrary code in different languages, comparable to Python or JS bindings routinely generated to wrap C++ sorts. All in moveable commonplace C++.

This can be a Massive Furry 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. Right this moment 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 function with further capabilities (simply as we added to constexpr over time to fill that out) and learn to use it in our applications and construct environments.

You possibly can see an instance of the brand new reflection capabilities from InfoQ showcasing turning an enum into string values at compile time:

enum Shade { purple, inexperienced, blue }; 

static_assert(enum_to_string(Shade::purple) == "purple");

template  

template 

    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

Herb Sutter C++ 26 Weblog

InfoQ Article

C++ Normal Homepage

You possibly can study extra concerning the new options coming in C++ 26 within the video beneath.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles