{"id":5759,"date":"2019-08-16T07:56:28","date_gmt":"2019-08-16T07:56:28","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-more-non-rules-and-myths\/"},"modified":"2023-06-26T10:03:35","modified_gmt":"2023-06-26T10:03:35","slug":"c-core-guidelines-more-non-rules-and-myths","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-more-non-rules-and-myths\/","title":{"rendered":"C++ Core Guidelines: More Non-Rules and Myths"},"content":{"rendered":"<p>Demystifying non-rules and myths in C++ is a laborious but essential job. The goal is simple: use the powerful tool C++ appropriately.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5755\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/sao-jorge-1503439_1280.png\" alt=\"sao jorge 1503439 1280\" width=\"600\" height=\"328\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/sao-jorge-1503439_1280.png 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/sao-jorge-1503439_1280-300x164.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/sao-jorge-1503439_1280-1024x559.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/sao-jorge-1503439_1280-768x419.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>By the way, did you notice that my family name qualifies me, in particular,&nbsp;to write about this demystification? Anyway, here are the rules from the C++ core guidelines for today.<\/p>\n<ul>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rnr-two-phase-init\">NR.5: Don\u2019t: Don\u2019t do substantive work in a constructor; instead, use two-phase initialization<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rnr-goto-exit\">NR.6: Don\u2019t: Place all cleanup actions at the end of a function and <code class=\"highlighter-rouge no-highlight\">goto exit<\/code><\/a><\/li>\n<\/ul>\n<h3><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rnr-two-phase-init\">NR.5: Don\u2019t: Don\u2019t do substantive work in a constructor; instead, use two-phase initialization<\/a><\/h3>\n<p>This is the job of a constructor: <strong>After the constructor is executed, you should have a fully initialized object.<\/strong> For that reason, the following code snippet from the guidelines is terrible.&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: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Picture<\/span>\r\n{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> mx;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> my;\r\n    <span style=\"color: #007788; font-weight: bold;\">char<\/span> <span style=\"color: #555555;\">*<\/span> data;\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    Picture(<span style=\"color: #007788; font-weight: bold;\">int<\/span> x, <span style=\"color: #007788; font-weight: bold;\">int<\/span> y)\r\n    {\r\n        mx <span style=\"color: #555555;\">=<\/span> x,\r\n        my <span style=\"color: #555555;\">=<\/span> y;\r\n        data <span style=\"color: #555555;\">=<\/span> nullptr;\r\n    }\r\n\r\n    <span style=\"color: #555555;\">~<\/span>Picture()\r\n    {\r\n        Cleanup();\r\n    }\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> Init()\r\n    {\r\n        <span style=\"color: #0099ff; font-style: italic;\">\/\/ invariant checks<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span> (mx <span style=\"color: #555555;\">&lt;=<\/span> <span style=\"color: #ff6600;\">0<\/span> <span style=\"color: #555555;\">||<\/span> my <span style=\"color: #555555;\">&lt;=<\/span> <span style=\"color: #ff6600;\">0<\/span>) {\r\n            <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">false<\/span>;\r\n        }\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span> (data) {\r\n            <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">false<\/span>;\r\n        }\r\n        data <span style=\"color: #555555;\">=<\/span> (<span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span>) malloc(x<span style=\"color: #555555;\">*<\/span>y<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>));\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> data <span style=\"color: #555555;\">!=<\/span> nullptr;\r\n    }\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> Cleanup()                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    {\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span> (data) free(data);\r\n        data <span style=\"color: #555555;\">=<\/span> nullptr;\r\n    }\r\n};\r\n\r\nPicture <span style=\"color: #cc00ff;\">picture<\/span>(<span style=\"color: #ff6600;\">100<\/span>, <span style=\"color: #ff6600;\">0<\/span>); <span style=\"color: #0099ff; font-style: italic;\">\/\/ not ready-to-use picture here<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ this will fail..                   \/\/ (1)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (<span style=\"color: #555555;\">!<\/span>picture.Init()) {\r\n    puts(<span style=\"color: #cc3300;\">\"Error, invalid picture\"<\/span>);\r\n}\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ now have a invalid picture object instance.<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">picture(100, 0)<\/span> is not fully initialized; therefore, all operations on the picture inline (1) operate on an invalid picture. The solution to this problem is as simple as effective: put all initialization into the constructor.<\/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: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Picture<\/span>\r\n{\r\n    <span style=\"color: #007788; font-weight: bold;\">size_t<\/span> mx;\r\n    <span style=\"color: #007788; font-weight: bold;\">size_t<\/span> my;\r\n    vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">&gt;<\/span> data;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">size_t<\/span> <span style=\"color: #cc00ff;\">check_size<\/span>(<span style=\"color: #007788; font-weight: bold;\">size_t<\/span> s)\r\n    {\r\n        <span style=\"color: #0099ff; font-style: italic;\">\/\/ invariant check<\/span>\r\n        Expects(s <span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #ff6600;\">0<\/span>);\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> s;\r\n    }\r\n\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ even more better would be a class for a 2D Size as one single parameter<\/span>\r\n    Picture(<span style=\"color: #007788; font-weight: bold;\">size_t<\/span> x, <span style=\"color: #007788; font-weight: bold;\">size_t<\/span> y)\r\n        <span style=\"color: #555555;\">:<\/span> mx(check_size(x))\r\n        , my(check_size(y))\r\n        <span style=\"color: #0099ff; font-style: italic;\">\/\/ now we know x and y have a valid size<\/span>\r\n        , data(mx <span style=\"color: #555555;\">*<\/span> my <span style=\"color: #555555;\">*<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>)) <span style=\"color: #0099ff; font-style: italic;\">\/\/ will throw std::bad_alloc on error<\/span>\r\n    {\r\n        <span style=\"color: #0099ff; font-style: italic;\">\/\/ picture is ready-to-use<\/span>\r\n    }\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ compiler generated dtor does the job. (also see C.21)<\/span>\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Additionally, <span style=\"font-family: courier new, courier;\">data<\/span> is in the second example a<span style=\"font-family: courier new, courier;\"> std::vector<\/span> and not a raw pointer. This means the<span style=\"font-family: courier new, courier;\"> Cleanup<\/span> function (line 2) from the first example is not necessary anymore because the compiler will automatically clean up. Thanks to the static function <span style=\"font-family: courier new, courier;\">check_size,<\/span> the constructor can validate its arguments. But this is not the end of the benefits modern C++ gives up.&nbsp;<\/p>\n<p>Often you use constructors to set the default behavior of an object. Please don&#8217;t do it. Directly set the default behavior of an object in the class body. For example, compare the following classes <span style=\"font-family: courier new, courier;\">Widget<\/span> and <span style=\"font-family: courier new, courier;\">WidgetImpro.<\/span><\/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;\">\/\/ classMemberInitialiserWidget.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;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Widget<\/span>{\r\n  <span style=\"color: #9999ff;\">public:<\/span>\r\n    Widget()<span style=\"color: #555555;\">:<\/span> width(<span style=\"color: #ff6600;\">640<\/span>), height(<span style=\"color: #ff6600;\">480<\/span>), frame(<span style=\"color: #336666;\">false<\/span>), visible(<span style=\"color: #336666;\">true<\/span>) {}\r\n    <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Widget(<span style=\"color: #007788; font-weight: bold;\">int<\/span> w)<span style=\"color: #555555;\">:<\/span> width(w), height(getHeight(w)), frame(<span style=\"color: #336666;\">false<\/span>), visible(<span style=\"color: #336666;\">true<\/span>){}\r\n    Widget(<span style=\"color: #007788; font-weight: bold;\">int<\/span> w, <span style=\"color: #007788; font-weight: bold;\">int<\/span> h)<span style=\"color: #555555;\">:<\/span> width(w), height(h), frame(<span style=\"color: #336666;\">false<\/span>), visible(<span style=\"color: #336666;\">true<\/span>){}\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> show(){ 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> width <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"x\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> height\r\n                           <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", frame: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> frame <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", visible: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> visible\r\n                           <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n     }\r\n  <span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getHeight(<span style=\"color: #007788; font-weight: bold;\">int<\/span> w){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> w<span style=\"color: #555555;\">*<\/span><span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">4<\/span>; }\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> width;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> height;\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> frame;\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> visible;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">WidgetImpro<\/span>{\r\n  <span style=\"color: #9999ff;\">public:<\/span>\r\n    WidgetImpro(){}\r\n    <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> WidgetImpro(<span style=\"color: #007788; font-weight: bold;\">int<\/span> w)<span style=\"color: #555555;\">:<\/span> width(w), height(getHeight(w)){}\r\n    WidgetImpro(<span style=\"color: #007788; font-weight: bold;\">int<\/span> w, <span style=\"color: #007788; font-weight: bold;\">int<\/span> h)<span style=\"color: #555555;\">:<\/span> width(w), height(h){}\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> show(){ 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> width <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"x\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> height\r\n                           <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", frame: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> frame <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", visible: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> visible\r\n                           <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n\r\n  <span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getHeight(<span style=\"color: #007788; font-weight: bold;\">int<\/span> w){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> w <span style=\"color: #555555;\">*<\/span> <span style=\"color: #ff6600;\">3<\/span> <span style=\"color: #555555;\">\/<\/span> <span style=\"color: #ff6600;\">4<\/span>; }\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> width <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">640<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> height <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">480<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> frame <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">false<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> visible <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">true<\/span>;\r\n};\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  Widget wVGA;\r\n  Widget wSVGA(<span style=\"color: #ff6600;\">800<\/span>);\r\n  Widget wHD(<span style=\"color: #ff6600;\">1280<\/span>, <span style=\"color: #ff6600;\">720<\/span>);\r\n\r\n  wVGA.show();\r\n  wSVGA.show();\r\n  wHD.show();\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  WidgetImpro wImproVGA;\r\n  WidgetImpro wImproSVGA(<span style=\"color: #ff6600;\">800<\/span>);\r\n  WidgetImpro wImproHD(<span style=\"color: #ff6600;\">1280<\/span>, <span style=\"color: #ff6600;\">720<\/span>);\r\n\r\n  wImproVGA.show();\r\n  wImproSVGA.show();\r\n  wImproHD.show();\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>Both classes behave the same.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5756\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/classMemberInitialiserWidget.png\" alt=\"classMemberInitialiserWidget\" width=\"400\" height=\"207\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/classMemberInitialiserWidget.png 515w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/classMemberInitialiserWidget-300x156.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>The difference is that the constructors for the class <span style=\"font-family: courier new, courier;\">WidgetImpro<\/span> are way more comfortable to use and extend. When you add a new variable to both classes, in the case of WidgetImpro, you have only to edit one place, but each constructor in the case of the class <span style=\"font-family: courier new, courier;\">Widget<\/span> class is affected. Here is the picture I have in mind when I design a new class:&nbsp;<strong>Define the default behavior of each object in the class body. Use explicit constructors to vary the default behavior.<\/strong><\/p>\n<p>Done? No!<\/p>\n<p>Often you use an init function to put standard initialization or validation stuff into one place. You follow the necessary DRY (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Don%27t_repeat_yourself\"><strong>D<\/strong>on&#8217;t <strong>R<\/strong>epeat <strong>Y<\/strong>ourself<\/a>) principle but automatically break the other important principle that your object should be fully initialized after the constructor call. How can you solve this riddle? Quite easy. Since C++11, we have constructor delegation. This means putting the standard initialization and validation stuff into one intelligent constructor and using the other constructors as a kind of wrapper-constructors. Here is my idea translated to code.<\/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;\">\/\/ constructorDelegation.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cmath&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Degree<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Degree(<span style=\"color: #007788; font-weight: bold;\">int<\/span> deg){                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    degree <span style=\"color: #555555;\">=<\/span> deg <span style=\"color: #555555;\">%<\/span> <span style=\"color: #ff6600;\">360<\/span>;\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (degree <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">0<\/span>) degree <span style=\"color: #555555;\">+=<\/span> <span style=\"color: #ff6600;\">360<\/span>;\r\n  }\r\n  \r\n  Degree() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n                                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> <span style=\"color: #cc00ff;\">Degree<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span> deg)<span style=\"color: #555555;\">:<\/span>Degree(<span style=\"color: #006699; font-weight: bold;\">static_cast<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(ceil(deg))) {}  \r\n\r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> getDegree() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> degree; }\r\n\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> degree{};                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/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  Degree degree;\r\n  Degree degree10(<span style=\"color: #ff6600;\">10<\/span>);\r\n  Degree degree45(<span style=\"color: #ff6600;\">45<\/span>);\r\n  Degree degreeMinus315(<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">315<\/span>);\r\n  Degree degree405(<span style=\"color: #ff6600;\">405<\/span>);\r\n  Degree degree44(<span style=\"color: #ff6600;\">44.45<\/span>);\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Degree(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> degree.getDegree() <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;\">\"Degree(10): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> degree10.getDegree() <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;\">\"Degree(45): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> degree45.getDegree() <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;\">\"Degree(-315): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> degreeMinus315.getDegree() <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;\">\"Degree(405): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> degree405.getDegree() <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;\">\"Degree(44.45): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> degree44.getDegree() <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>The expression int degree{} (line) 1 value-initialized the degree to 0. The constructor in line 2 is quite intelligent. It transforms each degree into a unit circle. The constructor, taking a <span style=\"font-family: courier new, courier;\">double,<\/span> uses this constructor. For completeness, here is the output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5757\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/constructorDelegation.png\" alt=\"constructorDelegation\" width=\"300\" height=\"189\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/constructorDelegation.png 408w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/constructorDelegation-300x188.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<\/p>\n<h3><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rnr-goto-exit\">NR.6: Don\u2019t: Place all cleanup actions at the end of a function and <code class=\"highlighter-rouge no-highlight\">goto exit<\/code><\/a><\/h3>\n<p>Okay, we can do better with the following code from the guidelines:<\/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: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">do_something<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> n)\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (n <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">100<\/span>) <span style=\"color: #006699; font-weight: bold;\">goto<\/span> exit;\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span> p <span style=\"color: #555555;\">=<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span>) malloc(n);\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n<span style=\"color: #9999ff;\">exit:<\/span>\r\n    free(p);\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>By the way. Do you spot the error? The jump <span style=\"font-family: courier new, courier;\">goto exit<\/span> bypasses the definition of the pointer <span style=\"font-family: courier new, courier;\">p<\/span>.<\/p>\n<p>What I often saw in legacy C-code was code structures like this.<\/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: #009999;\">\/\/ lifecycle.c<br \/><br \/>#include &lt;stdio.h&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">initDevice<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> mess){\r\n  printf(<span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">INIT: %s<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>,mess);\r\n}\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">work<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> mess){\r\n  printf(<span style=\"color: #cc3300;\">\"WORKING: %s\"<\/span>,mess);\r\n}\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">shutDownDevice<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> mess){\r\n  printf(<span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">SHUT DOWN: %s<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>,mess);\r\n}\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(<span style=\"color: #007788; font-weight: bold;\">void<\/span>){\r\n \r\n  initDevice(<span style=\"color: #cc3300;\">\"DEVICE 1\"<\/span>);\r\n  work(<span style=\"color: #cc3300;\">\"DEVICE1\"<\/span>);\r\n  {\r\n    initDevice(<span style=\"color: #cc3300;\">\"DEVICE 2\"<\/span>);\r\n    work(<span style=\"color: #cc3300;\">\"DEVICE2\"<\/span>);\r\n    shutDownDevice(<span style=\"color: #cc3300;\">\"DEVICE 2\"<\/span>);\r\n  }\r\n  work(<span style=\"color: #cc3300;\">\"DEVICE 1\"<\/span>);\r\n  shutDownDevice(<span style=\"color: #cc3300;\">\"DEVICE 1\"<\/span>);\r\n \r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This is very error-prone but also a typical code. Each device usage consists of three steps: initialization, usage, and release. Honestly, this is the job of RAII.<\/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: #009999;\">\/\/ lifecycle.cpp<br \/><br \/>#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n \r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Device<\/span>{\r\n  <span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string resource;\r\n  <span style=\"color: #9999ff;\">public:<\/span>\r\n    Device(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> res)<span style=\"color: #555555;\">:<\/span>resource(res){\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<\/span><span style=\"color: #cc3300;\">INIT: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> resource <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\".<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n    }\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> work() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"WORKING: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> resource <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n    <span style=\"color: #555555;\">~<\/span>Device(){\r\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"SHUT DOWN: \"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span> resource <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};\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n \r\n \r\n  Device resGuard1{<span style=\"color: #cc3300;\">\"DEVICE 1\"<\/span>};\r\n  resGuard1.work();\r\n \r\n  {\r\n    Device resGuard2{<span style=\"color: #cc3300;\">\"DEVICE 2\"<\/span>};\r\n    resGuard2.work();\r\n  }\r\n  resGuard1.work();\r\n \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Initialize the resource in the constructor and release it in the destructor. First, you can not forget to initialize the object, and second, the compiler takes care of the release of the resource. The output of both programs is equivalent:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5758\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/lifecycle.png\" alt=\"lifecycle\" width=\"250\" height=\"239\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/lifecycle.png 321w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/lifecycle-300x287.png 300w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>You can find more information on RAII in my previous post: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-when-raii-breaks\">C++ Core Guidelines: When RAII breaks<\/a>.<\/p>\n<h2>More Myths<\/h2>\n<p>I&#8217;m sure this is not the end of the fight, and you know more non-rules and myths about C++. Please write a letter to <a href=\"mailto:rainer.grimm@modernescpp.de\">rainer.grimm@modernescpp.de<\/a>. Describe the myth and present, if possible, your solution. I try to make a post from your content and add your name if you like it. I&#8217;m inquisitive about your ideas.<\/p>\n<h2>What&#8217;s next<\/h2>\n<p>Only one rule to non-rules and myths is left in the C++ core guidelines. I hope for your input. <a href=\"mailto:rainer.grimm@modernescpp.de\"><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Demystifying non-rules and myths in C++ is a laborious but essential job. The goal is simple: use the powerful tool C++ appropriately.<\/p>\n","protected":false},"author":21,"featured_media":5755,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[467],"class_list":["post-5759","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-myths"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5759","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=5759"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5759\/revisions"}],"predecessor-version":[{"id":6776,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5759\/revisions\/6776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5755"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}