{"id":5973,"date":"2020-08-17T18:57:14","date_gmt":"2020-08-17T18:57:14","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/volatile-and-other-small-improvements-in-c-20\/"},"modified":"2023-06-26T09:46:02","modified_gmt":"2023-06-26T09:46:02","slug":"volatile-and-other-small-improvements-in-c-20","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/volatile-and-other-small-improvements-in-c-20\/","title":{"rendered":"volatile and Other Small Improvements in C++20"},"content":{"rendered":"<p>Today, I complete my tour through the C++20 core language features with a few small improvements. One interesting of these minor improvements is that most of <span style=\"font-family: 'courier new', courier;\">volatile<\/span> has been deprecated.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5945\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage.png\" alt=\"TimelineCpp20CoreLanguage\" width=\"650\" height=\"233\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<h2><span style=\"font-family: 'courier new', courier;\">volatile<\/span><\/h2>\n<p>The abstract in the proposal <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2018\/p1152r0.html\">P1152R0<\/a> briefly describes the changes that <span style=\"font-family: 'courier new', courier;\">volatile <\/span>undergoes: &#8220;<em>The proposed deprecation preserves the useful parts of&nbsp;volatile, and removes the dubious \/ already broken ones. This paper aims at breaking at compile-time code which is today subtly broken at runtime or through a compiler update.<\/em> &#8220;<\/p>\n<p>Before I show you what semantic of <span style=\"font-family: 'courier new', courier;\">volatile<\/span> is preserved, I want to start with the deprecated features:<\/p>\n<ol>\n<li>Deprecate <span style=\"font-family: 'courier new', courier;\">volatile<\/span> compound assignment, and pre\/post increment\/decrement<\/li>\n<li>Deprecate <span style=\"font-family: 'courier new', courier;\">volatile<\/span> qualification of function parameters or return types<\/li>\n<li>Deprecate <span style=\"font-family: 'courier new', courier;\">volatile<\/span> qualifiers in a structured binding declaration<\/li>\n<\/ol>\n<p>If you want to know all the sophisticated details, I strongly suggest you watch the CppCon 2019 talk &#8220;<a href=\"https:\/\/www.youtube.com\/watch?v=KJW_DLaVXIY\">Deprecating volatile<\/a>&#8221; from JF Bastien. Here are a few examples from the talk referring to used numbers (1) to (3).<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">(<span style=\"color: #ff6600;\">1<\/span>)\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> neck, tail;\r\n<span style=\"color: #006699; font-weight: bold;\">volatile<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> brachiosaur;\r\nbrachiosaur <span style=\"color: #555555;\">=<\/span> neck;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK, a volatile store<\/span>\r\ntail <span style=\"color: #555555;\">=<\/span> brachiosaur;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK, a volatile load<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ deprecated: does this access brachiosaur once or twice<\/span>\r\ntail <span style=\"color: #555555;\">=<\/span> brachiosaur <span style=\"color: #555555;\">=<\/span> neck;\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ deprecated: does this access brachiosaur once or twice<\/span>\r\nbrachiosaur <span style=\"color: #555555;\">+=<\/span> neck;\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ OK, a volatile load, an addition, a volatile store<\/span>\r\nbrachiosau <span style=\"color: #555555;\">=<\/span> brachiosaur <span style=\"color: #555555;\">+<\/span> neck;\r\n\r\n<span style=\"color: #009999;\">#########################################<\/span>\r\n(<span style=\"color: #ff6600;\">2<\/span>)\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ deprecated: a volatile return type has no meaning<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">volatile<\/span> <span style=\"color: #006699; font-weight: bold;\">struct<\/span> amber jurassic();\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ deprecated: volatile parameters aren't meaningful to the<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/             caller, volatile only applies within the function<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">trex<\/span>(<span style=\"color: #006699; font-weight: bold;\">volatile<\/span> <span style=\"color: #007788; font-weight: bold;\">short<\/span> left_arm, <span style=\"color: #006699; font-weight: bold;\">volatile<\/span> <span style=\"color: #007788; font-weight: bold;\">short<\/span> right_arm);\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ OK, the pointer isn't volatile, the data is opints to is<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">fly<\/span>(<span style=\"color: #006699; font-weight: bold;\">volatile<\/span> <span style=\"color: #006699; font-weight: bold;\">struct<\/span> pterosaur<span style=\"color: #555555;\">*<\/span> pterandon);\r\n\r\n<span style=\"color: #009999;\">########################################<\/span>\r\n(<span style=\"color: #ff6600;\">3<\/span>)\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> linhenykus { <span style=\"color: #006699; font-weight: bold;\">volatile<\/span> <span style=\"color: #007788; font-weight: bold;\">short<\/span> forelimb; };\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">park<\/span>(linhenykus alvarezsauroid) {\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ deprecated: doe the binding copy the foreelimbs?<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> [what_is_this] <span style=\"color: #555555;\">=<\/span> alvarezsauroid;\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I didn&#8217;t answer the crucial question: When should you use <span style=\"font-family: 'courier new', courier;\">volatile<\/span>?&nbsp; A note from the C++ standard says that &#8220;<em><span style=\"font-family: 'courier new', courier;\">volatile<\/span> is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation.<\/em>&#8221; This means for a single thread of execution that, the compiler must perform load or store operations in the executable as often as they occur in the source code.&nbsp;<span style=\"font-family: 'courier new', courier;\">volatile<\/span> operations, therefore, cannot be eliminated or reordered. Consequently, you can use <span style=\"font-family: 'courier new', courier;\">volatile<\/span> objects to communicate with a signal handler but not with another thread of execution.<\/p>\n<p><strong>To summarize, <span style=\"font-family: 'courier new', courier;\">volatile<\/span>&nbsp;avoids aggressive optimization and has no multithreading semantics.<\/strong><\/p>\n<p>I present the remaining minor improvements with a short example that runs in the Compiler Explorer.<\/p>\n<\/p>\n<h2>Range-based for-loop with Initializers<\/h2>\n<p>With C++20, you can directly use a range-based for-loop with an initializer.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ rangeBasedForLoopInitializer.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> vec <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>vector{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>}; <span style=\"color: #006699; font-weight: bold;\">auto<\/span> v <span style=\"color: #555555;\">:<\/span> vec) {  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    }\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> initList <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>}; <span style=\"color: #006699; font-weight: bold;\">auto<\/span> e <span style=\"color: #555555;\">:<\/span> initList) {  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n        e <span style=\"color: #555555;\">*=<\/span> e;\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> e <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    }\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n    \r\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;\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> str <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"Hello World\"<\/span>s; <span style=\"color: #006699; font-weight: bold;\">auto<\/span> c<span style=\"color: #555555;\">:<\/span> str) {        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> c <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    }\r\n     \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The range-based for-loop uses in line (1) a <span style=\"font-family: 'courier new', courier;\">std::vector<\/span>, in line (2) a <span style=\"font-family: 'courier new', courier;\">std::initializer_list,<\/span> and line (3) a <span style=\"font-family: 'courier new', courier;\">std::string<\/span>. Additionally, in lines (1) and line (2), I apply automatic type deduction for class templates we have had since C++17. Instead of <span style=\"font-family: 'courier new', courier;\">std::vector&lt;int&gt;<\/span> and <span style=\"font-family: 'courier new', courier;\">std::initalizer_list&lt;int&gt;<\/span>, I write <span style=\"font-family: 'courier new', courier;\">std::vector<\/span> and <span style=\"font-family: 'courier new', courier;\">std::initializer_list<\/span>.&nbsp;<\/p>\n<p>With GCC 10.2 and <a href=\"https:\/\/godbolt.org\/z\/9c3rGs\">Compiler Explorer<\/a>, I get the expected output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5968\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/rangeBasedForLoopWithInitializer.png\" alt=\"rangeBasedForLoopWithInitializer\" width=\"200\" height=\"101\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/>&nbsp;<\/p>\n<h2><span style=\"font-family: 'courier new', courier;\">virtual constexpr<\/span> Functions<\/h2>\n<p>A <span style=\"font-family: 'courier new', courier;\">constexpr<\/span> function has the potential to run at compile-time but can also be executed at run-time. Consequently, you can make a <span style=\"font-family: 'courier new', courier;\">constexpr<\/span> function with C++20 <span style=\"font-family: 'courier new', courier;\">virtual<\/span>. Both directions are possible. Neither can a <span style=\"font-family: 'courier new', courier;\">virtual<\/span> <span style=\"font-family: 'courier new', courier;\">constexpr<\/span> function override a&nbsp; non-<span style=\"font-family: 'courier new', courier;\">constexpr<\/span> function nor can a virtual non-<span style=\"font-family: 'courier new', courier;\">constexpr<\/span> function override a&nbsp;&nbsp;<span style=\"font-family: 'courier new', courier;\">constexpr<\/span>&nbsp;<span style=\"font-family: 'courier new', courier;\">virtual<\/span> function. I want to emphasize that override implies that the regarding function of a base class is <span style=\"font-family: 'courier new', courier;\">virtual<\/span>.&nbsp;<\/p>\n<p>The following program shows both combinations.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ virtualConstexpr.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> X1 {\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> f() <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> X2<span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> X1 {\r\n    constexpr <span style=\"color: #007788; font-weight: bold;\">int<\/span> f() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">2<\/span>; }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> X3<span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> X2 {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> f() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">3<\/span>; }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> X4<span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> X3 {\r\n    constexpr <span style=\"color: #007788; font-weight: bold;\">int<\/span> f() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">4<\/span>; }\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    \r\n    X1<span style=\"color: #555555;\">*<\/span> x1 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> X4;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"x1-&gt;f(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x1<span style=\"color: #555555;\">-&gt;<\/span>f() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    X4 x4;\r\n    X1<span style=\"color: #555555;\">&amp;<\/span> x2 <span style=\"color: #555555;\">=<\/span> x4;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"x2.f(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x2.f() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Line (1) uses virtual dispatch (late binding) via a pointer, and line (2) uses virtual dispatch via reference. Once more, here is the output with GCC 10.2 and the <a href=\"https:\/\/godbolt.org\/z\/5qvdvz\">Compiler Explorer<\/a>.<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5969\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/virtualConstexpr.png\" alt=\"virtualConstexpr\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"139\" height=\"60\" \/><\/h2>\n<h2>The new Character Type for UTF-8 Strings:<span style=\"font-family: 'courier new', courier;\"> char8_t<\/span><\/h2>\n<p>Additionally, to the character types <span style=\"font-family: 'courier new', courier;\">char16_t<\/span> and <span style=\"font-family: 'courier new', courier;\">char32_t<\/span> from C++11, C++20 gets the new character type <span style=\"font-family: 'courier new', courier;\">char8_t.char8_t <\/span>is large enough to represent any UTF-8 code unit (8 bits). It has the same size, signedness, and alignment as an <span style=\"font-family: 'courier new', courier;\">unsigned char<\/span>, but is a distinct type.&nbsp;<\/p>\n<p>Consequently, C++20 has a new typedef for the character type<span style=\"font-family: 'courier new', courier;\"> char8_t<\/span>&nbsp;(1) and a new UTF-8 string literal (2).<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std<span style=\"color: #555555;\">::<\/span>u8string<span style=\"color: #555555;\">:<\/span> std<span style=\"color: #555555;\">::<\/span>basic_string<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char8_t<\/span><span style=\"color: #555555;\">&gt;<\/span> (<span style=\"color: #ff6600;\">1<\/span>)\r\nu8<span style=\"color: #cc3300;\">\"Hello World\"<\/span>                           (<span style=\"color: #ff6600;\">2<\/span>)\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The following program shows the straightforward usage of <span style=\"font-family: 'courier new', courier;\">char8_t<\/span>:<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ char8Str.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">char8_t<\/span><span style=\"color: #555555;\">*<\/span> char8Str <span style=\"color: #555555;\">=<\/span> u8<span style=\"color: #cc3300;\">\"Hello world\"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>basic_string<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char8_t<\/span><span style=\"color: #555555;\">&gt;<\/span> char8String <span style=\"color: #555555;\">=<\/span> u8<span style=\"color: #cc3300;\">\"helloWorld\"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>u8string char8String2 <span style=\"color: #555555;\">=<\/span> u8<span style=\"color: #cc3300;\">\"helloWorld\"<\/span>;\r\n    \r\n    char8String2 <span style=\"color: #555555;\">+=<\/span> u8<span style=\"color: #cc3300;\">\".\"<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"char8String.size(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> char8String.size() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"char8String2.size(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> char8String2.size() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    char8String2.replace(<span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #ff6600;\">5<\/span>, u8<span style=\"color: #cc3300;\">\"Hello \"<\/span>);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"char8String2.size(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> char8String2.size() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<section><span style=\"color: #242729; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: #ffffff; float: none;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><\/span><\/span><\/p>\n<p>Without further ado. Here is the output of the program on the<span style=\"color: #242729; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: #ffffff; float: none;\"><a href=\"https:\/\/godbolt.org\/z\/1fYfen\"> Compiler Explorer.<\/a><\/span><\/p>\n<\/section>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5970\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/char8Str.png\" alt=\"char8Str\" width=\"250\" height=\"84\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<h2><span style=\"font-family: 'courier new', courier;\">using enum<\/span> in Local Scopes&nbsp;<\/h2>\n<p>A <span style=\"font-family: courier new, courier;\">using enum<\/span> declaration introduces the enumerators of the named enumeration in the local scope.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ enumUsing.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string_view&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">enum<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Color<\/span> {\r\n    red,\r\n    green, \r\n    blue\r\n};\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>string_view toString(Color col) {\r\n  <span style=\"color: #006699; font-weight: bold;\">switch<\/span> (col) {\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">enum<\/span> Color;                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span> \r\n    <span style=\"color: #006699; font-weight: bold;\">case<\/span> red:   <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"red\"<\/span>;           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span> \r\n    <span style=\"color: #006699; font-weight: bold;\">case<\/span> green: <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"green\"<\/span>;         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span> \r\n    <span style=\"color: #006699; font-weight: bold;\">case<\/span> blue:  <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"blue\"<\/span>;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span> \r\n  }\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"unknown\"<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n   \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"toString(Color::red): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> toString(Color<span style=\"color: #555555;\">::<\/span>red) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">enum<\/span> Color;                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"toString(green): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> toString(green) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span> \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}  \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The <span style=\"font-family: 'courier new', courier;\">using enum<\/span> declaration in (1) introduces the enumerators of the scoped enumerations <span style=\"font-family: 'courier new', courier;\">Color<\/span> into the local scope. From then on, the enumerators can be used unscoped (2). This time, the only C++ compiler supporting<span style=\"font-family: 'courier new', courier;\"> using enum<\/span> is the Microsoft Compiler 19.24:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5971\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/enumUsing.png\" alt=\"enumUsing\" width=\"300\" height=\"158\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/enumUsing.png 1043w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/enumUsing-300x158.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/enumUsing-1024x541.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/enumUsing-768x406.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h1 style=\"color: #000000; font-family: 'Times New Roman'; font-style: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;\">Default member initializers for bit-fields<span style=\"font-family: 'arial black', 'avant garde';\"><\/span><\/h1>\n<p>First, what is a bit field? Here is the definition of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bit_field\">Wikipedia<\/a>: &#8220;<em>A&nbsp;bit field&nbsp;is a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Data_structure\" title=\"Data structure\">data structure<\/a>&nbsp;used in&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Computer_programming\" title=\"Computer programming\">computer programming<\/a>. It consists of a number of adjacent&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Computer_memory\" title=\"Computer memory\">computer memory<\/a>&nbsp;locations which have been allocated to hold a sequence of&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Bit\" title=\"Bit\">bits<\/a>, stored so that any single bit or group of bits within the set can be addressed.<a href=\"https:\/\/en.wikipedia.org\/wiki\/Bit_field#cite_note-BrummBrumm1988-1\">[1]<\/a><a href=\"https:\/\/en.wikipedia.org\/wiki\/Bit_field#cite_note-Oualline1997-2\">[2]<\/a>&nbsp;A bit field is most commonly used to represent&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Primitive_data_type\" title=\"Primitive data type\">integral types<\/a>&nbsp;of known, fixed bit-width.&#8221;<\/em><\/p>\n<p>With C++20, we can default initialize the members of a bit-field:<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ bitField.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Class11 {             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> j <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> k <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">3<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> l <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">4<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> m <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">6<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> BitField20 {          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">:<\/span> <span style=\"color: #ff6600;\">3<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> j <span style=\"color: #555555;\">:<\/span> <span style=\"color: #ff6600;\">4<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> k <span style=\"color: #555555;\">:<\/span> <span style=\"color: #ff6600;\">5<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">3<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> l <span style=\"color: #555555;\">:<\/span> <span style=\"color: #ff6600;\">6<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">4<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> m <span style=\"color: #555555;\">:<\/span> <span style=\"color: #ff6600;\">7<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">:<\/span> <span style=\"color: #ff6600;\">7<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">6<\/span>;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span> () {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(Class11): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(Class11) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(BitField20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(BitField20) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>According to the member of a class (1) with C++11, the members of the bit-field can have default initializers (2) with C++20. Finally, here is the output of the program with the Clang 10.0 compiler:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5972\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/bitField.png\" alt=\"bitField\" width=\"300\" height=\"166\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/bitField.png 968w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/bitField-300x166.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/bitField-768x425.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h2>A Short Writing Break<\/h2>\n<p>In the next fortnight, I will be in Italy, so I will not write a regular post.<br \/>If you want to read one of my more than 300 posts on modern C++, I created a visual tour through my blog. This visual tour explains the TOC, categories, tags, archive, and search system and should help you find the post you are looking for.<\/p>\n<p>Here you go&nbsp;<a href=\"https:\/\/youtu.be\/hrXoVSi0O28\">https:\/\/youtu.be\/hrXoVSi0O28<\/a>.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>After my short break, I continue my journey through C++20 with the new library. In particular, I will write about<span style=\"font-family: 'courier new', courier;\"> std::span<\/span>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I complete my tour through the C++20 core language features with a few small improvements. One interesting of these minor improvements is that most of volatile has been deprecated.<\/p>\n","protected":false},"author":21,"featured_media":5945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[428,458,457],"class_list":["post-5973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-constexpr","tag-enum","tag-volatile"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5973","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=5973"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5973\/revisions"}],"predecessor-version":[{"id":6730,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5973\/revisions\/6730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5945"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}