{"id":9898,"date":"2024-08-26T08:09:47","date_gmt":"2024-08-26T08:09:47","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9898"},"modified":"2025-07-03T16:23:56","modified_gmt":"2025-07-03T16:23:56","slug":"an-overview-of-c26-core-language","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/an-overview-of-c26-core-language\/","title":{"rendered":"An Overview of C++26: Core Language"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">C++26 has a lot to offer. Let me directly jump in and give you an overview.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"460\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/TimelineCpp2Core-1030x460.png\" alt=\"\" class=\"wp-image-9944\" style=\"width:800px;height:auto\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/TimelineCpp2Core-1030x460.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/TimelineCpp2Core-300x134.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/TimelineCpp2Core-768x343.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/TimelineCpp2Core-705x315.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/TimelineCpp2Core.png 1107w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This image gives you the first idea of what&#8217;s inside or outside C++26. Writing a blog post about the future is always challenging because the design freeze of C++26 is in the first quarter of 2025.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fishing in Murky Waters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This image should only give you the first idea of what&#8217;s inside or outside C++26. I will adapt the image while writing the next months about C++26 if necessary. For example, the three powerful features reflection, contracts, and <code>std::execution<\/code> made a big step towards their standardization. On the contrary, I intentionally ignore pattern matching on the image.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Very impactful features such as reflection or contracts implement the agile idea of the minimum viable product. &#8220;<em>A <strong>minimum viable product (MVP)<\/strong> is a version of a product with just enough features to be usable by early customers who can then provide feedback for future product development.<\/em>&#8221; (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Minimum_viable_product\">https:\/\/en.wikipedia.org\/wiki\/Minimum_viable_product<\/a>). This means C++26 is only the starting point for features such as reflection or contracts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If possible, I show you the feature in action. Many features are already implemented in the brand-new <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/compiler_support\">C++ compilers<\/a>. For the rest, I hope for prototype implementations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let me start with the core language.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Core Language<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Reflection<\/h3>\n\n\n\n<p class=\"has-small-font-size wp-block-paragraph\"><strong>Reflection <\/strong>is the ability of a program to examine, introspect, and modify its structure and behavior. This makes compile-time programming in C++ much more powerful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I don&#8217;t want to annoy you with too much theory in this overview post. Therefore, I will show you my favorite example from the reflection proposal <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2024\/p2996r5.html\">P2996R5<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A question I often have to answer in my classes is: How could I convert an enumerator to a string? <\/p>\n\n\n\n<div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ enumString.cpp<\/span><br><br><span style=\"color: #009999\">#include &lt;iostream><\/span><br><span style=\"color: #009999\">#include &lt;experimental\/meta><\/span><br><span style=\"color: #009999\">#include &lt;string><\/span><br><span style=\"color: #009999\">#include &lt;type_traits><\/span><br><br><span style=\"color: #0099FF; font-style: italic\">\/\/ start 'expand' definition<\/span><br><span style=\"color: #006699; font-weight: bold\">namespace<\/span> __impl {<br>  <span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">auto<\/span>... vals<span style=\"color: #555555\">><\/span><br>  <span style=\"color: #006699; font-weight: bold\">struct<\/span> replicator_type {<br>    <span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> F<span style=\"color: #555555\">><\/span><br>      constexpr <span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #006699; font-weight: bold\">operator<\/span><span style=\"color: #555555\">>><\/span>(F body) <span style=\"color: #006699; font-weight: bold\">const<\/span> {<br>        (body.<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #006699; font-weight: bold\">operator<\/span>()<span style=\"color: #555555\">&lt;<\/span>vals<span style=\"color: #555555\">><\/span>(), ...);<br>      }<br>  };<br><br>  <span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">auto<\/span>... vals<span style=\"color: #555555\">><\/span><br>  replicator_type<span style=\"color: #555555\">&lt;<\/span>vals...<span style=\"color: #555555\">><\/span> replicator <span style=\"color: #555555\">=<\/span> {};<br>}<br><br><span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> R<span style=\"color: #555555\">><\/span><br>consteval <span style=\"color: #006699; font-weight: bold\">auto<\/span> expand(R range) {<br>  std<span style=\"color: #555555\">::<\/span>vector<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>meta<span style=\"color: #555555\">::<\/span>info<span style=\"color: #555555\">><\/span> args;<br>  <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> r <span style=\"color: #555555\">:<\/span> range) {<br>    args.push_back(std<span style=\"color: #555555\">::<\/span>meta<span style=\"color: #555555\">::<\/span>reflect_value(r));<br>  }<br>  <span style=\"color: #006699; font-weight: bold\">return<\/span> substitute(<span style=\"color: #555555\">^<\/span>__impl<span style=\"color: #555555\">::<\/span>replicator, args);<br>}<br><span style=\"color: #0099FF; font-style: italic\">\/\/ end 'expand' definition<\/span><br><br><span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> E<span style=\"color: #555555\">><\/span><br>  requires std<span style=\"color: #555555\">::<\/span>is_enum_v<span style=\"color: #555555\">&lt;<\/span>E<span style=\"color: #555555\">><\/span>                      <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span><br>constexpr std<span style=\"color: #555555\">::<\/span>string enum_to_string(E value) {<br>  std<span style=\"color: #555555\">::<\/span>string result <span style=\"color: #555555\">=<\/span> <span style=\"color: #CC3300\">\"&lt;unnamed>\"<\/span>;<br>  [<span style=\"color: #555555\">:<\/span>expand(std<span style=\"color: #555555\">::<\/span>meta<span style=\"color: #555555\">::<\/span>enumerators_of(<span style=\"color: #555555\">^<\/span>E))<span style=\"color: #555555\">:<\/span>] <span style=\"color: #555555\">>><\/span>    <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span><br>  [<span style=\"color: #555555\">&amp;<\/span>]<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">auto<\/span> e<span style=\"color: #555555\">><\/span>{<br>    <span style=\"color: #006699; font-weight: bold\">if<\/span> (value <span style=\"color: #555555\">==<\/span> [<span style=\"color: #555555\">:<\/span>e<span style=\"color: #555555\">:<\/span>]) {<br>      result <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>meta<span style=\"color: #555555\">::<\/span>identifier_of(e);       <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span><br>    }<br>  };<br>  <span style=\"color: #006699; font-weight: bold\">return<\/span> result;<br>}<br><br><br><span style=\"color: #007788; font-weight: bold\">int<\/span> main() {<br><br>    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">'\\n'<\/span>;<br><br>    <span style=\"color: #006699; font-weight: bold\">enum<\/span> Color { red, green, blue };<br>    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">\"enum_to_string(Color::red): \"<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> enum_to_string(Color<span style=\"color: #555555\">::<\/span>red) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">'\\n'<\/span>;<br>    <span style=\"color: #0099FF; font-style: italic\">\/\/ std::cout &lt;&lt; \"enum_to_string(42): \" &lt;&lt; enum_to_string(42) &lt;&lt; '\\n'; <\/span><br><br>    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">'\\n'<\/span>;<br><br>}<br><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This example uses the experimental features (<code>std::meta<\/code>) of the standard. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the output of the program:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"403\" height=\"50\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/enumString.png\" alt=\"\" class=\"wp-image-9914\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/enumString.png 403w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/08\/enumString-300x37.png 300w\" sizes=\"auto, (max-width: 403px) 100vw, 403px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let me say a few words about the function, template <code>enum_to_string<\/code>. The function <code>expand <\/code>is a workaround. The function call <code>enum_to_string(Color(42))<\/code> breaks because the function requires an enum.: <code>requires std::is_enum_v&lt;E&gt;<\/code> (line 1).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Line (1) applies a reflection operator (<code>^E<\/code>) and calls the meta function <code>enum_to_std::meta::enumerators_of(^E))<\/code>. Finally, the so-called <em>splicer <\/em>(<code>[: ref<\/code>l<code> :]<\/code>) produces in line (2) grammatical elements for reflection. The second meta function in line (3) creates the string:<code>std:meta::identifier_of(e))<\/code>. The <code>consteval <\/code>meta functions are executed at compile time, and so is the reflection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Contracts<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>contract <\/strong>specifies interfaces for software components precisely and checkably. These software components are functions that fulfill preconditions, postconditions, and invariants.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a straightforward example from the proposal <a href=\"https:\/\/isocpp.org\/files\/papers\/P2900R6.pdf\"><strong>P2900<\/strong><\/a>.<\/p>\n\n\n\n<div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">int <span style=\"color: #CC00FF\">f<\/span>(<span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #007788; font-weight: bold\">int<\/span> x)<br>  pre (x <span style=\"color: #555555\">!=<\/span> <span style=\"color: #FF6600\">1<\/span>) <span style=\"color: #0099FF; font-style: italic\">\/\/ a precondition assertion<\/span><br>  post(r <span style=\"color: #555555\">:<\/span> r <span style=\"color: #555555\">!=<\/span> <span style=\"color: #FF6600\">2<\/span>) <span style=\"color: #0099FF; font-style: italic\">\/\/ a postcondition assertion; r refers to the return value of f<\/span><br>{<br>  contract_assert (x <span style=\"color: #555555\">!=<\/span> <span style=\"color: #FF6600\">3<\/span>); <span style=\"color: #0099FF; font-style: italic\">\/\/ an assertion statement<\/span><br>  <span style=\"color: #006699; font-weight: bold\">return<\/span> x;<br>}<br><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The function f has a precondition, postcondition, and invariant. The precondition is checked before the function invocation, the postcondition after the function invocation, and the invariant precisely at the point of its invocation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Invoking the function with arguments 1, 2, or 3 causes a contract violation. There are different ways to react to a contract violation.<\/p>\n\n\n\n<div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">g<\/span>()<br>{<br>  f(<span style=\"color: #FF6600\">0<\/span>); <span style=\"color: #0099FF; font-style: italic\">\/\/ no contract violation<\/span><br>  f(<span style=\"color: #FF6600\">1<\/span>); <span style=\"color: #0099FF; font-style: italic\">\/\/ violates precondition assertion of f<\/span><br>  f(<span style=\"color: #FF6600\">2<\/span>); <span style=\"color: #0099FF; font-style: italic\">\/\/ violates postcondition assertion of f<\/span><br>  f(<span style=\"color: #FF6600\">3<\/span>); <span style=\"color: #0099FF; font-style: italic\">\/\/ violates assertion statement within f<\/span><br>  f(<span style=\"color: #FF6600\">4<\/span>); <span style=\"color: #0099FF; font-style: italic\">\/\/ no contract violation<\/span><br>}<br><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The core language of C++26 offers more than just reflection and contracts. Let me name them and present a code snippet from the corresponding proposals.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Placeholder and extended character set<\/strong><\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #006699; font-weight: bold\">auto<\/span> [x, y, _] <span style=\"color: #555555\">=<\/span> f();\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The _ stands for I don&#8217;t care and it can be used more than once.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>static_assert <\/code>extension<\/strong><\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">static_assert(<span style=\"color: #006699; font-weight: bold\">sizeof<\/span>(S) <span style=\"color: #555555\">==<\/span> <span style=\"color: #FF6600\">1<\/span>,\nstd<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;Unexpected sizeof: expected 1, got {}&quot;<\/span>, <span style=\"color: #006699; font-weight: bold\">sizeof<\/span>(S))\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Template improvements<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">There are many template improvements in C++26. My favorite one is pack indexing:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span>... T<span style=\"color: #555555\">&gt;<\/span>\nconstexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> first_plus_last(T... values) <span style=\"color: #555555\">-&gt;<\/span> T...[<span style=\"color: #FF6600\">0<\/span>] {\n  <span style=\"color: #006699; font-weight: bold\">return<\/span> T...[<span style=\"color: #FF6600\">0<\/span>](values...[<span style=\"color: #FF6600\">0<\/span>] <span style=\"color: #555555\">+<\/span> values...[<span style=\"color: #006699; font-weight: bold\">sizeof<\/span>...(values)<span style=\"color: #555555\">-<\/span><span style=\"color: #FF6600\">1<\/span>]);\n}\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> main() {\n  <span style=\"color: #0099FF; font-style: italic\">\/\/first_plus_last(); \/\/ ill formed<\/span>\n  static_assert(first_plus_last(<span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">2<\/span>, <span style=\"color: #FF6600\">10<\/span>) <span style=\"color: #555555\">==<\/span> <span style=\"color: #FF6600\">11<\/span>);\n}\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>delete <\/code>with reason<\/strong><\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #006699; font-weight: bold\">delete<\/span>(<span style=\"color: #CC3300\">&quot;Should have a reason&quot;<\/span>);\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In my next post, I will provide an overview of the C++26 library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++26 has a lot to offer. Let me directly jump in and give you an overview. This image gives you the first idea of what&#8217;s inside or outside C++26. Writing a blog post about the future is always challenging because the design freeze of C++26 is in the first quarter of 2025. Fishing in Murky [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":9901,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"[]"},"categories":[559],"tags":[],"class_list":["post-9898","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c26-blog"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=9898"}],"version-history":[{"count":39,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9898\/revisions"}],"predecessor-version":[{"id":10852,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9898\/revisions\/10852"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/9901"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=9898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}