{"id":5402,"date":"2018-03-09T17:01:47","date_gmt":"2018-03-09T17:01:47","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-more-about-control-structures\/"},"modified":"2023-06-26T11:55:09","modified_gmt":"2023-06-26T11:55:09","slug":"c-core-guidelines-more-about-control-structures","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-more-about-control-structures\/","title":{"rendered":"C++ Core Guidelines: More about Control Structures"},"content":{"rendered":"<p>My last German post C++<a href=\"https:\/\/www.heise.de\/-3985896\">&nbsp;Core Guidelines: To Switch or not to Switch, that is the Question<\/a>&nbsp;got a lot of attention. To use a hash table instead of a switch statement seems to be a highly emotional topic. So I change my original plan. Today, I will present different kinds of control structures. I will start with the if and switch statements, continue with the hash table, and end with dynamic and static polymorphism. Additionally, I will mark a few remarks about performance and maintainability.&nbsp;<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5400\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/640px-Schienennetz_Schweiz.svg.png\" alt=\"640px Schienennetz Schweiz.svg\" width=\"500\" height=\"294\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/640px-Schienennetz_Schweiz.svg.png 640w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/640px-Schienennetz_Schweiz.svg-300x176.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The classical control structure is the if statement; this is my starting point.<\/p>\n<h3>if statement<\/h3>\n<p>Here is the simple program that I will implement with different control structures.<\/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;\">\/\/ dispatchIf.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&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;\">MessageSeverity<\/span>{                                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    information,\r\n    warning,\r\n    fatal,\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeElapsedTime<\/span>(){                                   \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> now <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> diff <span style=\"color: #555555;\">=<\/span> now <span style=\"color: #555555;\">-<\/span> start;\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> diff.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" sec. elapsed: \"<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeInformation<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"information\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeWarning<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"warning\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeUnexpected<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"unexpected\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeMessage<\/span>(MessageSeverity messServer){               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\t\r\n    writeElapsedTime();                                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n\t\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (MessageSeverity<span style=\"color: #555555;\">::<\/span>information <span style=\"color: #555555;\">==<\/span> messServer){\r\n\t    writeInformation();\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span> <span style=\"color: #006699; font-weight: bold;\">if<\/span> (MessageSeverity<span style=\"color: #555555;\">::<\/span>warning <span style=\"color: #555555;\">==<\/span> messServer){\r\n\t    writeWarning();\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span>{\r\n\t    writeUnexpected();\r\n    }\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    writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>information);\r\n    writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>warning);\r\n    writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>fatal);\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 function <span style=\"font-family: 'courier new', courier;\">writeMessage<\/span> in line (1)&nbsp; displays the elapsed time in seconds (3) since the program&#8217;s start and a log message. It uses an enumeration (2) for the message severity. I use the start time (4) and the actual time (5) to calculate the elapsed time. As the name suggested, the<span style=\"font-family: 'courier new', courier;\"> std::steady_clock&nbsp;<\/span>cannot be adjusted; therefore, it is the right choice for this measurement. The key part of the program is part of the function <span style=\"font-family: 'courier new', courier;\">writeMessage<\/span> (1), in which I decide which message should be displayed. In this case, I used if-else statements.&nbsp;<\/p>\n<p>To make it right, I had to look up the syntax for the if-else statement.&nbsp;<\/p>\n<p>Here is the output of the program:<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5401\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/dispatchIf.png\" alt=\"dispatchIf\" width=\"400\" height=\"171\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/dispatchIf.png 783w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/dispatchIf-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/dispatchIf-768x329.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;I will skip the output for the remaining examples. Besides the numbers, it is always the same.<\/p>\n<\/p>\n<h3>switch statement<\/h3>\n<p>The following program is quite similar to the previous one. Only the implementation of the function <span style=\"font-family: 'courier new', courier;\">writeMessage<\/span> changed.&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;\">\/\/ dispatchSwitch.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&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;\">MessageSeverity<\/span>{\r\n    information,\r\n    warning,\r\n    fatal,\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeElapsedTime<\/span>(){\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> now <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> diff <span style=\"color: #555555;\">=<\/span> now <span style=\"color: #555555;\">-<\/span> start;\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> diff.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" sec. elapsed: \"<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeInformation<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"information\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeWarning<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"warning\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeUnexpected<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"unexpected\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeMessage<\/span>(MessageSeverity messSever){\r\n\t\r\n    writeElapsedTime();\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">switch<\/span>(messSever){\r\n        <span style=\"color: #006699; font-weight: bold;\">case<\/span> MessageSeverity:<span style=\"color: #555555;\">:<\/span>information<span style=\"color: #555555;\">:<\/span>\r\n            writeInformation();\r\n            <span style=\"color: #006699; font-weight: bold;\">break<\/span>;\r\n        <span style=\"color: #006699; font-weight: bold;\">case<\/span> MessageSeverity:<span style=\"color: #555555;\">:<\/span>warning<span style=\"color: #555555;\">:<\/span>\r\n            writeWarning();\r\n            <span style=\"color: #006699; font-weight: bold;\">break<\/span>;\r\n        <span style=\"color: #9999ff;\">default:<\/span>\r\n            writeUnexpected();\r\n            <span style=\"color: #006699; font-weight: bold;\">break<\/span>;\r\n  }\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    writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>information);\r\n    writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>warning);\r\n    writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>fatal);\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>I will make it short. Let&#8217;s continue with the hash table.<\/p>\n<h3>Hashtable<\/h3>\n<p>For a more elaborate discussion of the switch statement and the hash table, read my last post: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-to-switch-or-not-to-switch-that-is-the-question\">C++ Core Guidelines: To Switch or not to Switch, that is the Question<\/a>.<\/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;\">\/\/ dispatchHashtable.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;functional&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;unordered_map&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;\">MessageSeverity<\/span>{\r\n  information,\r\n  warning,\r\n  fatal,\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeElapsedTime<\/span>(){\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> now <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> diff <span style=\"color: #555555;\">=<\/span> now <span style=\"color: #555555;\">-<\/span> start;\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> diff.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" sec. elapsed: \"<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeInformation<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"information\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeWarning<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"warning\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeUnexpected<\/span>(){ std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"unexpected\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; }\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>unordered_map<span style=\"color: #555555;\">&lt;<\/span>MessageSeverity, std<span style=\"color: #555555;\">::<\/span>function<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span>()<span style=\"color: #555555;\">&gt;&gt;<\/span> mess2Func{\r\n    {MessageSeverity<span style=\"color: #555555;\">::<\/span>information, writeInformation},\r\n    {MessageSeverity<span style=\"color: #555555;\">::<\/span>warning, writeWarning},\r\n    {MessageSeverity<span style=\"color: #555555;\">::<\/span>fatal, writeUnexpected}\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeMessage<\/span>(MessageSeverity messServer){\r\n\t\r\n\twriteElapsedTime();\r\n\t\r\n\tmess2Func[messServer]();\r\n\t\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  writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>information);\r\n  writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>warning);\r\n  writeMessage(MessageSeverity<span style=\"color: #555555;\">::<\/span>fatal);\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>Is this the end? No? In C++, we have dynamic and static polymorphism, as a few of my readers mentioned in their discussion. With the if-else or the switch statement, I used an enumerator to dispatch the correct case. The key of my hash table behaves similarly.&nbsp;<\/p>\n<p>Dynamic or static polymorphism is different. Instead of an enumerator or a key for dispatching the right action, I use objects which decide autonomously at runtime (dynamic polymorphism) or compile-time (static polymorphism) what should be done.&nbsp;<\/p>\n<p>Let&#8217;s continue with dynamic polymorphism.<\/p>\n<h3>Dynamic polymorphism<\/h3>\n<p>Not, the decision logic is encoded in the type hierarchy.<\/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;\">\/\/ dispatchDynamicPolymorphism.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeElapsedTime<\/span>(){\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> now <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> diff <span style=\"color: #555555;\">=<\/span> now <span style=\"color: #555555;\">-<\/span> start;\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> diff.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" sec. elapsed: \"<\/span>;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> MessageSeverity{                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\t<span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> writeMessage() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n\t\tstd<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"unexpected\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\t}\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> MessageInformation<span style=\"color: #555555;\">:<\/span> MessageSeverity{     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> writeMessage() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n\t\tstd<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"information\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\t}\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> MessageWarning<span style=\"color: #555555;\">:<\/span> MessageSeverity{         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> writeMessage() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n\t\tstd<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"warning\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\t}\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> MessageFatal<span style=\"color: #555555;\">:<\/span> MessageSeverity{};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeMessageReference<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MessageSeverity<span style=\"color: #555555;\">&amp;<\/span> messServer){\r\n\t\r\n\twriteElapsedTime();\r\n\tmessServer.writeMessage();\r\n\t\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeMessagePointer<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MessageSeverity<span style=\"color: #555555;\">*<\/span> messServer){\r\n\t\r\n\twriteElapsedTime();\r\n\tmessServer<span style=\"color: #555555;\">-&gt;<\/span>writeMessage();\r\n\t\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    MessageInformation messInfo;\r\n    MessageWarning messWarn;\r\n    MessageFatal messFatal;\r\n  \r\n    MessageSeverity<span style=\"color: #555555;\">&amp;<\/span> messRef1 <span style=\"color: #555555;\">=<\/span> messInfo;            \r\n    MessageSeverity<span style=\"color: #555555;\">&amp;<\/span> messRef2 <span style=\"color: #555555;\">=<\/span> messWarn;\r\n    MessageSeverity<span style=\"color: #555555;\">&amp;<\/span> messRef3 <span style=\"color: #555555;\">=<\/span> messFatal;\r\n  \r\n    writeMessageReference(messRef1);              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    writeMessageReference(messRef2);\r\n    writeMessageReference(messRef3);\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n    MessageSeverity<span style=\"color: #555555;\">*<\/span> messPoin1 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> MessageInformation;\r\n    MessageSeverity<span style=\"color: #555555;\">*<\/span> messPoin2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> MessageWarning;\r\n    MessageSeverity<span style=\"color: #555555;\">*<\/span> messPoin3 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> MessageFatal;\r\n  \r\n    writeMessagePointer(messPoin1);               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n    writeMessagePointer(messPoin2);\r\n    writeMessagePointer(messPoin3);\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 classes (1), (2), and (3) know what they have to display if used. The key idea is that the static type <span style=\"font-family: 'courier new', courier;\">MessageSeverity<\/span> differs from the dynamic type such as <span style=\"font-family: 'courier new', courier;\">MessageInformation<\/span>(4); therefore, the late binding will kick in, and the <span style=\"font-family: 'courier new', courier;\">writeMessage<\/span> methods (5), (6), and (7) of the dynamic types are used. Dynamic polymorphism requires a kind of indirection. You can use references (8) or pointers (9).&nbsp;<\/p>\n<p>From a performance perspective, we can do better and make the dispatch at compile time.<\/p>\n<h3>Static polymorphism&nbsp;&nbsp;<\/h3>\n<p>Static polymorphism is often called CRTP. CRTP stands for the C++ idiom <strong>C<\/strong>uriously <b>R<\/b>ecurring <b>T<\/b>emplate <b>P<\/b>attern. Curiously because a class derives this technique from a class template instantiation using itself as a template argument.<\/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;\">\/\/ dispatchStaticPolymorphism.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">writeElapsedTime<\/span>(){\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> now <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> diff <span style=\"color: #555555;\">=<\/span> now <span style=\"color: #555555;\">-<\/span> start;\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> diff.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" sec. elapsed: \"<\/span>;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> ConcreteMessage<span style=\"color: #555555;\">&gt;<\/span>                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> MessageSeverity{\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> writeMessage(){                                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">static_cast<\/span><span style=\"color: #555555;\">&lt;<\/span>ConcreteMessage<span style=\"color: #555555;\">*&gt;<\/span>(<span style=\"color: #006699; font-weight: bold;\">this<\/span>)<span style=\"color: #555555;\">-&gt;<\/span>writeMessageImplementation();\r\n  }\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> writeMessageImplementation() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"unexpected\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> MessageInformation<span style=\"color: #555555;\">:<\/span> MessageSeverity<span style=\"color: #555555;\">&lt;<\/span>MessageInformation<span style=\"color: #555555;\">&gt;<\/span>{\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> writeMessageImplementation() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"information\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> MessageWarning<span style=\"color: #555555;\">:<\/span> MessageSeverity<span style=\"color: #555555;\">&lt;<\/span>MessageWarning<span style=\"color: #555555;\">&gt;<\/span>{\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> writeMessageImplementation() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"warning\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> MessageFatal<span style=\"color: #555555;\">:<\/span> MessageSeverity<span style=\"color: #555555;\">&lt;<\/span>MessageFatal<span style=\"color: #555555;\">&gt;<\/span>{};     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> writeMessage(T<span style=\"color: #555555;\">&amp;<\/span> messServer){                       \r\n\t\r\n    writeElapsedTime();                                   \r\n    messServer.writeMessage();                            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\r\n\t\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    MessageInformation messInfo;\r\n    writeMessage(messInfo);\r\n    \r\n    MessageWarning messWarn;\r\n    writeMessage(messWarn);\r\n\t\r\n    MessageFatal messFatal;\r\n    writeMessage(messFatal);\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>In this case, all concrete classes (3), (4), and (5) derive from the base class <span style=\"font-family: 'courier new', courier;\">MessageSeverity<\/span>. The method <span style=\"font-family: 'courier new', courier;\">writeMessage<\/span> is a kind of interface that dispatches to the concrete implementations <span style=\"font-family: 'courier new', courier;\">writeMessageImplementation<\/span>.&nbsp; To make that happen, the object will be upcasted to the <span style=\"font-family: 'courier new', courier;\">ConcreteMessage<\/span>:&nbsp;<span style=\"font-family: 'courier new', courier;\">&nbsp;static_cast&lt;ConcreteMessage*&gt;(this)-&gt;writeMessageImplementation();. <\/span>This is the static dispatch at compile time; therefore, this technique is called static polymorphism.<\/p>\n<p>It took me time to get used to it, but applying the static polymorphism in line (6) is quite easy. If the&nbsp;curiously recurring template pattern is still curious to you, I wrote an article about it: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-is-still-lazy\">C++ is Lazy: CRTP<\/a><\/p>\n<p>To end my comparison, let me compare these various techniques.<\/p>\n<h2>My simple comparison<\/h2>\n<p>Let&#8217;s first look at your preferred way to implement and maintain a control structure. Depending on your experience as a C programmer, the if or switch statements seem pretty natural to you. If you have an interpreter background, you may prefer the hash table. With an object orientation background, dynamic polymorphism is your preferred way to implement the control structure. Static polymorphism, also called CRTP, is quite unique; therefore, it will take some time to get comfortable with it. Afterward, it is quite a pattern you have to use.&nbsp;<\/p>\n<p>I must mention the new context-sensitive identifiers override from the security perspective.&nbsp; It&nbsp;helps to express your intent to override a virtual method in your type hierarchy. If you make it wrong, the compiler will complain.&nbsp;<\/p>\n<p>Now to the more interesting question. What are the performance differences? I will only provide a rough idea without numbers. If you have a long series of if statements, this will become quite expensive because many comparisons are involved. The dynamic polymorphism and the hash table will be faster and in the same ballpark because, in both cases, a pointer indirection is involved. The switch statement and the static polymorphism make their decision at compile time; therefore, they are the two fastest control structures.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p><span style=\"color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: #ffffff; float: none;\">I hope I&#8217;m done with the discussion of the different control structures; therefore, I will in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-to-statements-and-arithmetic-rules\">next post<\/a> the last rules to statements and start with the rules for arithmetic expressions.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My last German post C++&nbsp;Core Guidelines: To Switch or not to Switch, that is the Question&nbsp;got a lot of attention. To use a hash table instead of a switch statement seems to be a highly emotional topic. So I change my original plan. Today, I will present different kinds of control structures. I will start [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":5400,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[472,491,489,490],"class_list":["post-5402","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-associative-containers","tag-control-structures","tag-if","tag-switch"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5402","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=5402"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5402\/revisions"}],"predecessor-version":[{"id":6836,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5402\/revisions\/6836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5400"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}