{"id":10203,"date":"2024-10-28T10:19:47","date_gmt":"2024-10-28T10:19:47","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10203"},"modified":"2025-07-04T15:09:45","modified_gmt":"2025-07-04T15:09:45","slug":"placeholders-and-extended-character-set","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/placeholders-and-extended-character-set\/","title":{"rendered":"Placeholders and Extended Character Set"},"content":{"rendered":"\n<p>Placeholders are a nice way to highlight variables that are no longer needed. Additionally, the character set of C++26 will be extended.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"537\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Time26Placeholder.png\" alt=\"\" class=\"wp-image-10211\" style=\"width:450px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Time26Placeholder.png 696w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Time26Placeholder-300x231.png 300w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Placeholders<\/h2>\n\n\n\n<p>Structured bindings are a C++17 feature that allows you to bind multiple variables to the elements of a structured object. <\/p>\n\n\n\n<p>The following program demonstrates using tuples and structured bindings to return and unpack multiple values from a function. <\/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\">\/\/ placeholder1.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;tuple&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #0099FF; font-style: italic\">\/\/ Function that returns three values<\/span>\nstd<span style=\"color: #555555\">::<\/span>tuple<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span>, std<span style=\"color: #555555\">::<\/span>string, <span style=\"color: #007788; font-weight: bold\">double<\/span><span style=\"color: #555555\">&gt;<\/span> getThreeValues() {\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> intValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">42<\/span>;\n    std<span style=\"color: #555555\">::<\/span>string strValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #CC3300\">&quot;example&quot;<\/span>;\n    <span style=\"color: #007788; font-weight: bold\">double<\/span> doubleValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">3.14<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">return<\/span> std<span style=\"color: #555555\">::<\/span>make_tuple(intValue, strValue, doubleValue);\n}\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> main() {\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Retrieve the three values using structured binding<\/span>\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> [intValue, strValue, doubleValue] <span style=\"color: #555555\">=<\/span> getThreeValues();\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Print the values<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Integer: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> intValue <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;String: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> strValue <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Double: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> doubleValue <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 <code>getThreeValues<\/code> is defined to return a tuple containing three different types of values: an <code>int<\/code>, a <code>std::string<\/code>, and a <code>double<\/code>. These values are then packed into a tuple using <code>std::make_tuple<\/code> and returned from the function.<\/p>\n\n\n\n<p>In the <code>main<\/code> function, the program retrieves the three values returned by <code>getThreeValues<\/code> using structured bindings. Structured bindings allow the program to unpack the tuple directly into three separate variables: <code>intValue<\/code>, <code>strValue<\/code>, and <code>doubleValue<\/code>. This makes the code more readable and easier to work with than manually unpacking the tuple.<\/p>\n\n\n\n<p> Sometimes, you don&#8217;t need all three values from the function <code>getThreeValues<\/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\">\/\/ placeholder2.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;tuple&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #0099FF; font-style: italic\">\/\/ Function that returns three values<\/span>\nstd<span style=\"color: #555555\">::<\/span>tuple<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span>, std<span style=\"color: #555555\">::<\/span>string, <span style=\"color: #007788; font-weight: bold\">double<\/span><span style=\"color: #555555\">&gt;<\/span> getThreeValues() {\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> intValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">42<\/span>;\n    std<span style=\"color: #555555\">::<\/span>string strValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #CC3300\">&quot;example&quot;<\/span>;\n    <span style=\"color: #007788; font-weight: bold\">double<\/span> doubleValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">3.14<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">return<\/span> std<span style=\"color: #555555\">::<\/span>make_tuple(intValue, strValue, doubleValue);\n}\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> main() {\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Retrieve the three values using structured binding<\/span>\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> [_, strValue, doubleValue] <span style=\"color: #555555\">=<\/span> getThreeValues();\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Print the values<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;String: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> strValue <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Double: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> doubleValue <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>This time, the <code>intValue <\/code>from the function <code>getThreeValues<\/code> is not needed in the subsequent code. By convention, I bind it to the underline. <\/p>\n\n\n\n<p>At the same time, this means that the compiler does not issue a warning because the variable<code> _ <\/code>is not used:<\/p>\n\n\n\n<p>Unfortunately, the intuitive _ can only be used once as an identifier. This changes with C++26. Now, it can be used as often as required.<\/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\">\/\/ placeholder3.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;tuple&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #0099FF; font-style: italic\">\/\/ Function that returns three values<\/span>\nstd<span style=\"color: #555555\">::<\/span>tuple<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span>, std<span style=\"color: #555555\">::<\/span>string, <span style=\"color: #007788; font-weight: bold\">double<\/span><span style=\"color: #555555\">&gt;<\/span> getThreeValues() {\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> intValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">42<\/span>;\n    std<span style=\"color: #555555\">::<\/span>string strValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #CC3300\">&quot;example&quot;<\/span>;\n    <span style=\"color: #007788; font-weight: bold\">double<\/span> doubleValue <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">3.14<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">return<\/span> std<span style=\"color: #555555\">::<\/span>make_tuple(intValue, strValue, doubleValue);\n}\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> main() {\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Retrieve the three values using structured binding<\/span>\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> [_, strValue, _] <span style=\"color: #555555\">=<\/span> getThreeValues();\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Print the values<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;String: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> strValue <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>In this variant, neither the <code>intValue <\/code>nor the <code>doubleValue <\/code>from the function <code>getThreeValues<\/code> is not required. I consistently use two underscores.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"167\" height=\"37\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/placeholder-1.png\" alt=\"\" class=\"wp-image-10209\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Extended Character Set<\/h2>\n\n\n\n<p>Three new characters are available in the <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/charset#Basic_character_set\">basic character set<\/a>:<br><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"366\" height=\"151\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/addedCharacterSet.png\" alt=\"\" class=\"wp-image-10213\" style=\"width:300px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/addedCharacterSet.png 366w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/addedCharacterSet-300x124.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/figure>\n\n\n\n<p> The following program uses all three of them for raw string literals.<\/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\">\/\/ extendedCharacterset.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&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>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\">auto<\/span> raw1 <span style=\"color: #555555\">=<\/span> R<span style=\"color: #CC3300\">&quot;@(Hello<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">)@&quot;<\/span>;    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> raw2 <span style=\"color: #555555\">=<\/span> R<span style=\"color: #CC3300\">&quot;$(Hello<\/span><span style=\"color: #CC3300; font-weight: bold\">\\t<\/span><span style=\"color: #CC3300\">)$&quot;<\/span>;    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> raw3 <span style=\"color: #555555\">=<\/span> R<span style=\"color: #CC3300\">&quot;`(Hello<\/span><span style=\"color: #CC3300; font-weight: bold\">\\b<\/span><span style=\"color: #CC3300\">)`&quot;<\/span>;\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;raw1: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> raw1 <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;raw2: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> raw2 <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;raw3: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> raw3 <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 program then defines three raw string literals: <code>raw1<\/code>, <code>raw2<\/code>, and <code>raw3<\/code>. Raw string literals in C++ are enclosed in <code>R\"delimiter(...)delimiter\"<\/code>, where <code>delimiter<\/code> can be any sequence of characters. This allows the string to contain special characters like <code>\\n<\/code>, <code>\\t<\/code>, and <code>\\b<\/code> without needing to escape them.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>raw1<\/code> is defined as <code>R\"@(Hello\\n)@\"<\/code>, which contains the text <code>Hello\\n<\/code> without interpreting <code>\\n<\/code> as a newline character.<\/li>\n\n\n\n<li><code>raw2<\/code> is defined as <code>R\"$(Hello\\t)$<\/code>&#8220;, which contains the text <code>Hello\\t<\/code> without interpreting <code>\\t<\/code> as a tab character.<\/li>\n\n\n\n<li><code>raw3<\/code> is defined as <code>R\"`(Hello\\b)`\"<\/code>, which contains the text <code>Hello\\b<\/code> without interpreting <code>\\b<\/code> as a backspace character.<\/li>\n<\/ul>\n\n\n\n<p>Finally, 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=\"195\" height=\"113\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/extendedCharacterSet.png\" alt=\"\" class=\"wp-image-10215\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s next?<\/h2>\n\n\n\n<p>The core language of C++26 still offers improvements, such as pack indexing. I will write about this in the next blog post.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Placeholders are a nice way to highlight variables that are no longer needed. Additionally, the character set of C++26 will be extended. Placeholders Structured bindings are a C++17 feature that allows you to bind multiple variables to the elements of a structured object. The following program demonstrates using tuples and structured bindings to return and [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":10211,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[559],"tags":[562],"class_list":["post-10203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c26-blog","tag-placeholders"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10203","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=10203"}],"version-history":[{"count":13,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10203\/revisions"}],"predecessor-version":[{"id":10261,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10203\/revisions\/10261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/10211"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=10203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}