{"id":10223,"date":"2024-11-04T08:34:46","date_gmt":"2024-11-04T08:34:46","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10223"},"modified":"2025-07-04T14:57:28","modified_gmt":"2025-07-04T14:57:28","slug":"c26-core-language-small-improvements","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c26-core-language-small-improvements\/","title":{"rendered":"C++26 Core Language: Small Improvements"},"content":{"rendered":"\n<p>There are more small improvements in the C++26 language, which you should know.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"537\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Time26CoreSmallFeatures.png\" alt=\"\" class=\"wp-image-10226\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Time26CoreSmallFeatures.png 696w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Time26CoreSmallFeatures-300x231.png 300w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><code>static_assert <\/code>extension<\/strong><\/h2>\n\n\n\n<p>First, here&#8217;s the syntax of <code>static_assert <\/code>in C++11:<\/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%\">static_assert(compile time predicate, unevaluated string)\n<\/pre><\/div>\n\n\n\n<p>With C++26, the string can be a user-defined type having the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Has a <code>size()<\/code> method that produces an integer<\/em><\/li>\n\n\n\n<li><em>Has a <code>data() <\/code>method that produces a pointer of character type such that<\/em><\/li>\n\n\n\n<li><em>The elements in the<code> range [data(), data()+size()) <\/code>are valid.<\/em> (<a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2023\/p2741r3.pdf\">p2741r3<\/a>)<\/li>\n<\/ul>\n\n\n\n<p><code>static_assert <\/code>can now be used with a format string. Here&#8217;s a nice example from the proposal <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2023\/p2741r3.pdf\">p2741r3<\/a>. I made a complete program out of it.<\/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: #0099FF; font-style: italic\">\/\/ static_assert26.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;format&gt;<\/span>\n\n<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: #006699; font-weight: bold\">auto<\/span> Expected, <span style=\"color: #007788; font-weight: bold\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold\">long<\/span> Size <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">sizeof<\/span>(T)<span style=\"color: #555555\">&gt;<\/span>\nconstexpr <span style=\"color: #007788; font-weight: bold\">bool<\/span> ensure_size() {\n    static_assert(<span style=\"color: #006699; font-weight: bold\">sizeof<\/span>(T) <span style=\"color: #555555\">==<\/span> Expected, <span style=\"color: #CC3300\">&quot;Unexpected sizeof&quot;<\/span>);\n    <span style=\"color: #006699; font-weight: bold\">return<\/span> <span style=\"color: #336666\">true<\/span>;\n}\n\n<span style=\"color: #006699; font-weight: bold\">struct<\/span> S {\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> _{};\n};\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>boolalpha <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;C++11<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    static_assert(ensure_size<span style=\"color: #555555\">&lt;<\/span>S, <span style=\"color: #FF6600\">1<\/span><span style=\"color: #555555\">&gt;<\/span>()); \n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>boolalpha <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;C++26<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    static_assert(<span style=\"color: #006699; font-weight: bold\">sizeof<\/span>(S) <span style=\"color: #555555\">==<\/span> <span style=\"color: #FF6600\">1<\/span>,\n        std<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\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;    \n\n}\n<\/pre><\/div>\n\n\n\n<p>The function template <code>ensure_size<\/code> is defined to take three parameters: a type <code>T<\/code>, an expected size <code>Expected<\/code>, and an optional parameter <code>Size<\/code> which defaults to the size of <code>T<\/code>. Inside the function, a <code>static_assert<\/code> statement checks if the size of <code>T<\/code> is equal to <code>Expected<\/code>. If the sizes do not match, the compilation will fail with the message &#8220;<code>Unexpected sizeof<\/code>&#8220;. The function returns <code>true<\/code> if the assertion passes.<\/p>\n\n\n\n<p>The program then defines a simple structure <a href=\"command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2FC%3A%2FUsers%2Fseminar%2FDropbox%2Fblog%2Fressourcen%2FC%2B%2B26%2FSmallFeaturesCoreLanguage%2Fstatic_assert26.cpp%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A11%2C%22character%22%3A7%7D%7D%5D%2C%22377f0a43-ca2b-4204-ab6b-71e6e49af2ff%22%5D\"><code>S<\/code><\/a> containing a single integer member <a href=\"command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2FC%3A%2FUsers%2Fseminar%2FDropbox%2Fblog%2Fressourcen%2FC%2B%2B26%2FSmallFeaturesCoreLanguage%2Fstatic_assert26.cpp%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A12%2C%22character%22%3A8%7D%7D%5D%2C%22377f0a43-ca2b-4204-ab6b-71e6e49af2ff%22%5D\"><code>_<\/code><\/a>. This structure is used to demonstrate the <code>static_assert<\/code> functionality.<\/p>\n\n\n\n<p>In the <code>main<\/code> function, the program first prints &#8220;<code>C++11<\/code>&#8221; to the console with <code>std::boolalpha<\/code> to format boolean values as <code>true<\/code> or <code>false<\/code>. It then calls <code>static_assert with<\/code>, which checks if the size of <code>S <\/code>is 1 byte. Since the size of <code>S <\/code>is actually larger than 1 byte, this assertion will fail, causing a compilation error.<\/p>\n\n\n\n<p>Next, the program prints &#8220;<code>C++26<\/code>&#8221; to the console and uses another <code>static_assert<\/code>. This time <code>std::format <\/code>is used. If the size of <code>S<\/code> is not 1 byte but 4, the compilation will fail.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"525\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/static_assert26-1030x525.png\" alt=\"\" class=\"wp-image-10228\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/static_assert26-1030x525.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/static_assert26-300x153.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/static_assert26-768x392.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/static_assert26-705x360.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/static_assert26.png 1131w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p>When you study GCC error messages, you will find three errors. <code>std::format <\/code>is so far not <code>constexpr<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pack Indexing<\/h2>\n\n\n\n<p>Pack indexing may be your favorite template improvement if you are template metaprogramming friend.<\/p>\n\n\n\n<p>The following example is based on the proposal <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2023\/p2662r3.pdf\">P2662R3<\/a>.<\/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: #0099FF; font-style: italic\">\/\/ packIndexing.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n\n<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\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> <span style=\"color: #006699; font-weight: bold\">namespace<\/span> std<span style=\"color: #555555\">::<\/span>string_literals;\n\n    std<span style=\"color: #555555\">::<\/span>string hello <span style=\"color: #555555\">=<\/span> first_plus_last(<span style=\"color: #CC3300\">&quot;Hello&quot;<\/span>s, <span style=\"color: #CC3300\">&quot;world&quot;<\/span>s, <span style=\"color: #CC3300\">&quot;goodbye&quot;<\/span>s, <span style=\"color: #CC3300\">&quot;World&quot;<\/span>s); \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;hello: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> hello <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n\n    constexpr <span style=\"color: #007788; font-weight: bold\">int<\/span> sum <span style=\"color: #555555\">=<\/span> first_plus_last(<span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">2<\/span>, <span style=\"color: #FF6600\">10<\/span>);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;sum: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> sum <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n  \n}\n<\/pre><\/div>\n\n\n\n<p>The provided example is a function template that computes the sum of a parameter pack&#8217;s first and last elements.<\/p>\n\n\n\n<p>The function is defined as a template that takes a variadic number of parameters of any type&nbsp;<code>T<\/code>. The function&#8217;s return type is specified using a trailing return type syntax.<\/p>\n\n\n\n<p>The function body returns the sum of the first and last elements of the parameter pack. The expression&nbsp;<code>values...[0]<\/code>&nbsp;accesses the first element and&nbsp;<code>values...[sizeof...(values)-1]<\/code>&nbsp;accesses the last element. <\/p>\n\n\n\n<p> 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=\"209\" height=\"78\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/packindexing.png\" alt=\"\" class=\"wp-image-10236\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><code>delete <\/code>with reason<\/strong><\/h2>\n\n\n\n<p>With C++26, you can specify a reason for your <code>delete<\/code>. I assume this will become best practice. The following program shall make this clear.<\/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: #0099FF; font-style: italic\">\/\/ deleteReason.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n\n<span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">func<\/span>(<span style=\"color: #007788; font-weight: bold\">double<\/span>){}\n\n<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>\n<span style=\"color: #007788; font-weight: bold\">void<\/span> func(T) <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">delete<\/span>(<span style=\"color: #CC3300\">&quot;Only for double&quot;<\/span>);\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>(){\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    func(<span style=\"color: #FF6600\">3.14<\/span>);\n    func(<span style=\"color: #FF6600\">3.14f<\/span>);\n  \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"294\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/deleteReason.png\" alt=\"\" class=\"wp-image-10242\" style=\"width:600px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/deleteReason.png 838w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/deleteReason-300x105.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/deleteReason-768x269.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/deleteReason-705x247.png 705w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The function <code>func<\/code> is overloaded in two ways. The first overload is a regular function that takes a <code>double<\/code> as its parameter. This function can be called with a <code>double<\/code> argument without any issues.<\/p>\n\n\n\n<p>The second overload is a function template that can take any type as its parameter. However, this function is explicitly deleted using the <code>= delete<\/code> specifier with a custom message <code>\"Only for double\"<\/code>. This means that any instantiation of with a type other than <code>double<\/code> will result in a compilation error, and the provided message will be displayed.<\/p>\n\n\n\n<p>In the <code>main<\/code> function, the program calls <code>func<\/code> with the argument <code>3.14<\/code>, which is a <code>double<\/code>. This call is valid and will invoke the non-template overload of <code>func<\/code>.<\/p>\n\n\n\n<p>Next, the program attempts to call <code>func<\/code> with the argument <code>3.14f<\/code>, which is a <code>float<\/code>. Since there is no non-template overload of <code>func<\/code> that takes a <code>float<\/code>, the template function would be instantiated. However, because the template function is deleted for any type other than <code>double<\/code>, this call will result in a compilation error with the message <code>\"Only for double\"<\/code>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> What is Next?<\/h2>\n\n\n\n<p> I will directly jump into the C++26 library in my next blog post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are more small improvements in the C++26 language, which you should know. static_assert extension First, here&#8217;s the syntax of static_assert in C++11: static_assert(compile time predicate, unevaluated string) With C++26, the string can be a user-defined type having the following properties: static_assert can now be used with a format string. Here&#8217;s a nice example from [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":10226,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[559],"tags":[],"class_list":["post-10223","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\/10223","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=10223"}],"version-history":[{"count":20,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10223\/revisions"}],"predecessor-version":[{"id":10271,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10223\/revisions\/10271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/10226"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=10223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}