{"id":7848,"date":"2023-07-31T06:46:29","date_gmt":"2023-07-31T06:46:29","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=7848"},"modified":"2023-08-23T17:00:28","modified_gmt":"2023-08-23T17:00:28","slug":"c23-the-small-pearls-in-the-core-language","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c23-the-small-pearls-in-the-core-language\/","title":{"rendered":"C++23: The Small Pearls in the Core Language"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The C++23 core language has more to offer than deducing this. Today, I will write about the small pearls.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"579\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/Cpp23-1-1030x579.png\" alt=\"\" class=\"wp-image-7888\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/Cpp23-1-1030x579.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/Cpp23-1-300x169.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/Cpp23-1-768x432.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/Cpp23-1-705x397.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/Cpp23-1.png 1280w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Literal Suffixes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++23 provides new integral literal suffixes for (signed) <code>std::size_t<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Literal Suffix<\/strong><\/td><td><strong>Type<\/strong><\/td><td><strong>Example<\/strong><\/td><\/tr><tr><td><code>z<\/code> or<code> Z<\/code><\/td><td>signed<code> std::size_t<\/code><\/td><td><code>auto signed = -2023z;<\/code><\/td><\/tr><tr><td><code>z\/Z <\/code>and<code> u\/U<\/code><\/td><td><code>std::size_t<\/code><\/td><td>auto unsigned = 2023uz,<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><a rel=\"noreferrer noopener\" href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/size_t\" target=\"_blank\">std::size_t<\/a> (C++17) is an unsigned data type that can hold the maximum size of any type. It is commonly used for array indexing and loop counting. <\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let me start with a simple example to see the value of the new suffixes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Assume you want to iterate through a vector. For optimization reasons, you cache the vectors&#8217; size. <\/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: #009999\">#include &lt;vector&gt;<\/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>vector<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> v{<span style=\"color: #FF6600\">0<\/span>, <span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">2<\/span>, <span style=\"color: #FF6600\">3<\/span>};\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> i <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>, s <span style=\"color: #555555\">=<\/span> v.size(); i <span style=\"color: #555555\">&lt;<\/span> s; <span style=\"color: #555555\">++<\/span>i) {\n\t    <span style=\"color: #0099FF; font-style: italic\">\/* use both i and v[i] *\/<\/span>\n    }\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">When you compile the code, you get the following error message in the <a href=\"https:\/\/godbolt.org\/z\/8bnG9raoG\" data-type=\"URL\" data-id=\"https:\/\/godbolt.org\/z\/8bnG9raoG\">Compiler Explorer<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"915\" height=\"262\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral1-1.png\" alt=\"\" class=\"wp-image-7868\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral1-1.png 915w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral1-1-300x86.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral1-1-768x220.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral1-1-705x202.png 705w\" sizes=\"auto, (max-width: 915px) 100vw, 915px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The reason is that <code>auto <\/code>deduced <code>i<\/code> to <code>int <\/code>and <code>s<\/code> to<code> long unsigned int<\/code>. Consequentially, making both variables unsigned will also not fix the issue.<\/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: #009999\">#include &lt;vector&gt;<\/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>vector<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> v{<span style=\"color: #FF6600\">0<\/span>, <span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">2<\/span>, <span style=\"color: #FF6600\">3<\/span>};\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> i <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0u<\/span>, s <span style=\"color: #555555\">=<\/span> v.size(); i <span style=\"color: #555555\">&lt;<\/span> s; <span style=\"color: #555555\">++<\/span>i) {\n\t    <span style=\"color: #0099FF; font-style: italic\">\/* use both i and v[i] *\/<\/span>\n    }\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Now, the compiler deduces<code> i<\/code> to unsigned <code>int <\/code>but <code>s<\/code> still to <code>long unsigned int<\/code>.  The following screenshot shows the <a href=\"https:\/\/godbolt.org\/z\/6hdeh3jdM\" data-type=\"URL\" data-id=\"https:\/\/godbolt.org\/z\/6hdeh3jdM\">Compiler Explorers<\/a> error output once more.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"267\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral2-1030x267.png\" alt=\"\" class=\"wp-image-7870\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral2-1030x267.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral2-300x78.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral2-768x199.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral2-705x183.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/errorLiteral2.png 1035w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks to C++23, the new literal suffix <code>z <\/code>will fix this issue.<\/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: #009999\">#include &lt;vector&gt;<\/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>vector<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> v{<span style=\"color: #FF6600\">0<\/span>, <span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">2<\/span>, <span style=\"color: #FF6600\">3<\/span>};\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> i <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0u<\/span>z, s <span style=\"color: #555555\">=<\/span> v.size(); i <span style=\"color: #555555\">&lt;<\/span> s; <span style=\"color: #555555\">++<\/span>i) {\n\t    <span style=\"color: #0099FF; font-style: italic\">\/* use both i and v[i] *\/<\/span>\n    }\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This example is based on the proposal <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2020\/p0330r8.html\">P0330R8<\/a>. The proposal has more motivating examples for the new literal suffixes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">if <code>consteval<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>if consteva<\/code>l behaves like <code>if (std::is_constant_evaluated()) { }<\/code> but has a  few advantages:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>No header <code>&lt;type_traits&gt;<\/code> is required.<\/li>\n\n\n\n<li>Has a more straightforward syntax like <code>std::is_constant_evaluated<\/code>.<\/li>\n\n\n\n<li>Can be used to invoke an immediate function.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Let me add a few words. <code>std::is_constant_evaluated<\/code> is a C++20 feature that detects if a <code>constexpr <\/code>function is evaluated during compile time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/is_constant_evaluated\" data-type=\"URL\" data-id=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/is_constant_evaluated\">cppreference.com\/is_constant_evaluated<\/a> has an excellent example: <\/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: #009999\">#include &lt;cmath&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;type_traits&gt;<\/span>\n \nconstexpr <span style=\"color: #007788; font-weight: bold\">double<\/span> <span style=\"color: #CC00FF\">power<\/span>(<span style=\"color: #007788; font-weight: bold\">double<\/span> b, <span style=\"color: #007788; font-weight: bold\">int<\/span> x)\n{\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (std<span style=\"color: #555555\">::<\/span>is_constant_evaluated() <span style=\"color: #555555\">&amp;&amp;<\/span> <span style=\"color: #555555\">!<\/span>(b <span style=\"color: #555555\">==<\/span> <span style=\"color: #FF6600\">0.0<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span> x <span style=\"color: #555555\">&lt;<\/span> <span style=\"color: #FF6600\">0<\/span>))\n    {\n        <span style=\"color: #0099FF; font-style: italic\">\/\/ A constant-evaluation context: Use a constexpr-friendly algorithm.<\/span>\n        <span style=\"color: #006699; font-weight: bold\">if<\/span> (x <span style=\"color: #555555\">==<\/span> <span style=\"color: #FF6600\">0<\/span>)\n            <span style=\"color: #006699; font-weight: bold\">return<\/span> <span style=\"color: #FF6600\">1.0<\/span>;\n        <span style=\"color: #007788; font-weight: bold\">double<\/span> r {<span style=\"color: #FF6600\">1.0<\/span>};\n        <span style=\"color: #007788; font-weight: bold\">double<\/span> p {x <span style=\"color: #555555\">&gt;<\/span> <span style=\"color: #FF6600\">0<\/span> <span style=\"color: #555555\">?<\/span> b <span style=\"color: #555555\">:<\/span> <span style=\"color: #FF6600\">1.0<\/span> <span style=\"color: #555555\">\/<\/span> b};\n        <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> u <span style=\"color: #555555\">=<\/span> <span style=\"color: #007788; font-weight: bold\">unsigned<\/span>(x <span style=\"color: #555555\">&gt;<\/span> <span style=\"color: #FF6600\">0<\/span> <span style=\"color: #555555\">?<\/span> x <span style=\"color: #555555\">:<\/span> <span style=\"color: #555555\">-<\/span>x); u <span style=\"color: #555555\">!=<\/span> <span style=\"color: #FF6600\">0<\/span>; u <span style=\"color: #555555\">\/=<\/span> <span style=\"color: #FF6600\">2<\/span>)\n        {\n            <span style=\"color: #006699; font-weight: bold\">if<\/span> (u <span style=\"color: #555555\">&amp;<\/span> <span style=\"color: #FF6600\">1<\/span>)\n                r <span style=\"color: #555555\">*=<\/span> p;\n            p <span style=\"color: #555555\">*=<\/span> p;\n        }\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> r;\n    }\n    <span style=\"color: #006699; font-weight: bold\">else<\/span>\n    {\n        <span style=\"color: #0099FF; font-style: italic\">\/\/ Let the code generator figure it out.<\/span>\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> std<span style=\"color: #555555\">::<\/span>pow(b, <span style=\"color: #007788; font-weight: bold\">double<\/span>(x));\n    }\n}\n \n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>()\n{\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ A constant-expression context<\/span>\n    constexpr <span style=\"color: #007788; font-weight: bold\">double<\/span> kilo <span style=\"color: #555555\">=<\/span> power(<span style=\"color: #FF6600\">10.0<\/span>, <span style=\"color: #FF6600\">3<\/span>);\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> n <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">3<\/span>;\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Not a constant expression, because n cannot be converted to an rvalue<\/span>\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ in a constant-expression context<\/span>\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Equivalent to std::pow(10.0, double(n))<\/span>\n    <span style=\"color: #007788; font-weight: bold\">double<\/span> mucho <span style=\"color: #555555\">=<\/span> power(<span style=\"color: #FF6600\">10.0<\/span>, n);\n \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> kilo <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> mucho <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>; <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The function <code>power<\/code> is <code>constexpr<\/code>. This means it has the potential to run at compile time. The first call of <code>power <\/code>causes a compile-time execution because the result is requested at compile-time: <code>constexpr double kilo = power(10.0, 1)<\/code>. On the contrary, the second call can only be performed at run time because the function argument <code>n<\/code> is no constant expression:  <code>double mucho = power(10.0, n<\/code>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks to <code>std::is_constant_evaluated<\/code>, different code is performed at compile time and run time. At compile time, it&#8217;s <code>if <\/code>branch is performed, and at run time, it&#8217;s <code>else <\/code>branch. Both <code>power <\/code>calls return 1000.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An immediate function is a <code>consteval <\/code>function. A <code>consteval <\/code>function is a function that can only run at compile time. You can read more about <code>consteval <\/code>functions in my C++20 post: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-consteval-and-constinit\/\" data-type=\"URL\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/c-20-consteval-and-constinit\/\">Two new Keywords in C++20: consteval and constinit<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Based on <code>consteval if<\/code>, you can implement <code>std::is_constant_evaluated<\/code>: <\/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%\">constexpr <span style=\"color: #007788; font-weight: bold\">bool<\/span> <span style=\"color: #CC00FF\">is_constant_evaluated<\/span>() {\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> consteval {\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> <span style=\"color: #336666\">true<\/span>;\n    } <span style=\"color: #006699; font-weight: bold\">else<\/span> {\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> <span style=\"color: #336666\">false<\/span>;\n    }\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">auto(x) and <code>auto{x}<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A generic way to obtain a copy of an object in C++ is <code>auto copy = x;<\/code>. This is fine but has one issue: <code>copy <\/code>is an lvalue, but you sometimes want a prvalue. prvalue is short for pure rvalue. A pure rvalue is an expression whose evaluation initializes an object. Read Barry&#8217;s post: <a href=\"https:\/\/medium.com\/@barryrevzin\/value-categories-in-c-17-f56ae54bccbe\" data-type=\"URL\" data-id=\"https:\/\/medium.com\/@barryrevzin\/value-categories-in-c-17-f56ae54bccbe\">Value Categories in C++17<\/a>, to learn more about value categories.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The calls <code>auto(x) <\/code>and <code>auto{x}<\/code> cast <code>x<\/code> into a prvalue as if passing <code>x<\/code> as a function argument by value.<code> auto(x)<\/code> and<code> auto{x}<\/code> perform a decay copy. What?  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I often have the question in my class about what decay means. Therefore, let me elaborate on this. Decay essentially means that some type information is lost when you copy a value. A typical example is a function taking its argument by value. Here are the flavors of decay:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Array-to-Pointer conversion<\/li>\n\n\n\n<li>Function-to-Pointer conversion<\/li>\n\n\n\n<li>Discarding const\/volatile qualifiers<\/li>\n\n\n\n<li>Removing references <\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The following program shows the four flavors of decay:<\/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\">\/\/ decay.cpp<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">decay<\/span>(<span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">*<\/span>, <span style=\"color: #007788; font-weight: bold\">void<\/span>(<span style=\"color: #555555\">*<\/span>)(<span style=\"color: #007788; font-weight: bold\">int<\/span>), <span style=\"color: #007788; font-weight: bold\">int<\/span>, <span style=\"color: #007788; font-weight: bold\">int<\/span> ) { }      <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">func<\/span>(<span style=\"color: #007788; font-weight: bold\">int<\/span>){}                                   <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> intArray[<span style=\"color: #FF6600\">5<\/span>]{<span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">2<\/span>, <span style=\"color: #FF6600\">3<\/span>, <span style=\"color: #FF6600\">4<\/span>, <span style=\"color: #FF6600\">5<\/span>};                <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #007788; font-weight: bold\">int<\/span> myInt{<span style=\"color: #FF6600\">5<\/span>};                            <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&amp;<\/span> myIntRef <span style=\"color: #555555\">=<\/span> myInt;                   <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n\n    decay(intArray, func, myInt, myIntRef);       \n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The function <code>decay <\/code>(5) requires a pointer to an <code>int<\/code>, a function pointer, and two <code>int<\/code>s. The first argument of the function call is an <code>int <\/code>array (1), the second a function (2), the third a <code>const int<\/code> (3), and the last is a <code>const int&amp;<\/code> (4). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The type-traits library has the function <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/decay\" data-type=\"URL\" data-id=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/decay\">std::decay<\/a><\/code>. This function enables you to apply these decay conversions directly on a type. Accordingly, these are the corresponding decay conversions using <code>std::decay<\/code>.<\/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\">\/\/ decayType.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;type_traits&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n     \n    <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ int[5] -&gt; int* <\/span>\n    static_assert(std<span style=\"color: #555555\">::<\/span>is_same<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>decay<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span>[<span style=\"color: #FF6600\">5<\/span>]<span style=\"color: #555555\">&gt;::<\/span>type, <span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">*&gt;::<\/span>value);             \n    \n    <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ void(int) -&gt; void(*)(int)<\/span>\n    static_assert(std<span style=\"color: #555555\">::<\/span>is_same<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>decay<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">void<\/span>(<span style=\"color: #007788; font-weight: bold\">int<\/span>)<span style=\"color: #555555\">&gt;::<\/span>type, <span style=\"color: #007788; font-weight: bold\">void<\/span>(<span style=\"color: #555555\">*<\/span>)(<span style=\"color: #007788; font-weight: bold\">int<\/span>)<span style=\"color: #555555\">&gt;::<\/span>value);  \n    \n    <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ const int -&gt; int<\/span>\n    static_assert(std<span style=\"color: #555555\">::<\/span>is_same<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>decay<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;::<\/span>type, <span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;::<\/span>value);           \n    \n    <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ const int&amp; -&gt; int<\/span>\n    static_assert(std<span style=\"color: #555555\">::<\/span>is_same<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>decay<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&amp;&gt;::<\/span>type, <span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;::<\/span>value);          \n\n}\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\">I&#8217;m not done with the small pearls in C++23. In my next post, I will continue my journey with C++23 core language features.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++23 core language has more to offer than deducing this. Today, I will write about the small pearls. Literal Suffixes C++23 provides new integral literal suffixes for (signed) std::size_t: Literal Suffix Type Example z or Z signed std::size_t auto signed = -2023z; z\/Z and u\/U std::size_t auto unsigned = 2023uz, std::size_t (C++17) is an [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":7888,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[378],"tags":[436],"class_list":["post-7848","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-23","tag-auto"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/7848","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=7848"}],"version-history":[{"count":42,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/7848\/revisions"}],"predecessor-version":[{"id":8035,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/7848\/revisions\/8035"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/7888"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=7848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=7848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=7848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}