{"id":5374,"date":"2018-01-20T08:43:49","date_gmt":"2018-01-20T08:43:49","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-more-rules-for-declarations\/"},"modified":"2023-06-26T11:58:48","modified_gmt":"2023-06-26T11:58:48","slug":"c-core-guidelines-more-rules-for-declarations","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-more-rules-for-declarations\/","title":{"rendered":"C++ Core Guidelines: More Rules for Declarations"},"content":{"rendered":"<p dir=\"ltr\" style=\"text-align: left;\">In this post, I will finish the rules for declarations. The remaining rules for declarations are not especially sophisticated but important for high code quality.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignright size-full wp-image-5373\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/declarationSmall.png\" alt=\"declarationSmall\" width=\"300\" height=\"363\" style=\"float: right;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/declarationSmall.png 423w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/declarationSmall-248x300.png 248w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Let&#8217;s start. Here is the first overview before we dive into the details.<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-const\">ES.25: Declare an object&nbsp;<code class=\"highlighter-rouge no-highlight\">const<\/code>&nbsp;or&nbsp;<code class=\"highlighter-rouge no-highlight\">constexpr<\/code>&nbsp;unless you want to modify its value later on<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-recycle\">ES.26: Don\u2019t use a variable for two unrelated purposes<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-stack\">ES.27: Use&nbsp;<code class=\"highlighter-rouge no-highlight\">std::array<\/code>&nbsp;or&nbsp;<code class=\"highlighter-rouge no-highlight\">stack_array<\/code>&nbsp;for arrays on the stack<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-lambda-init\">ES.28: Use lambdas for complex initialization, especially of&nbsp;<code class=\"highlighter-rouge no-highlight\">const<\/code>&nbsp;variables<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-macros\">ES.30: Don\u2019t use macros for program text manipulation<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-macros2\">ES.31: Don\u2019t use macros for constants or \u201cfunctions\u201d<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-ALL_CAPS\">ES.32: Use&nbsp;<code class=\"highlighter-rouge no-highlight\">ALL_CAPS<\/code>&nbsp;for all macro names<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-MACROS\">ES.33: If you must use macros, give them unique names<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-ellipses\">ES.34: Don\u2019t define a (C-style) variadic function<\/a><\/li>\n<\/ul>\n<p>In Python, there is an aphorism from the Zen of Python (Tim Peters): &#8220;Explicit is better than implicit&#8221;. This is a kind of meta-rule in Python for writing good code. This meta-rule holds, in particular, valid for the following two rules in the C++ core guidelines.<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-const\">ES.25: Declare an object&nbsp;<code class=\"highlighter-rouge no-highlight\">const<\/code>&nbsp;or&nbsp;<code class=\"highlighter-rouge no-highlight\">constexpr<\/code>&nbsp;unless you want to modify its value later on<\/a><\/h3>\n<p>Why should you use <span style=\"font-family: 'courier new', courier;\">const<\/span> or <span style=\"font-family: 'courier new', courier;\">constexpr<\/span> for your variable declaration if possible? I have a lot of good reasons:<\/p>\n<ul>\n<li>You express your intention.<\/li>\n<li>The variable cannot be changed by accident.<\/li>\n<li><span style=\"font-family: 'courier new', courier;\">const<\/span> or <span style=\"font-family: 'courier new', courier;\">constexpr<\/span> variables are, by definition, thread-safe.\n<ul>\n<li><span style=\"font-family: 'courier new', courier;\">const<\/span>: You have to guarantee that the variable is initialized in a thread-safe way.<\/li>\n<li><span style=\"font-family: 'courier new', courier;\">constexpr<\/span>: The C++ runtime guarantees that the variable is initialized in a thread-safe way.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-recycle\">ES.26: Don\u2019t use a variable for two unrelated purposes<\/a><\/h3>\n<p>Do you like such kind of code?<\/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;\">use<\/span>()\r\n{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> i;\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">20<\/span>; <span style=\"color: #555555;\">++<\/span>i) { <span style=\"color: #0099ff; font-style: italic;\">\/* ... *\/<\/span> }\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">200<\/span>; <span style=\"color: #555555;\">++<\/span>i) { <span style=\"color: #0099ff; font-style: italic;\">\/* ... *\/<\/span> } <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: i recycled<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I hope not. Put the declaration of <span style=\"font-family: 'courier new', courier;\">i<\/span> into the for loop, and you are fine. i will be bound to the lifetime of the for a loop.&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;\">use<\/span>()\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">20<\/span>; <span style=\"color: #555555;\">++<\/span>i) { <span style=\"color: #0099ff; font-style: italic;\">\/* ... *\/<\/span> }\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">200<\/span>; <span style=\"color: #555555;\">++<\/span>i) { <span style=\"color: #0099ff; font-style: italic;\">\/* ... *\/<\/span> } \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>With C++17, you can declare your<span style=\"font-family: 'courier new', courier;\"> i<\/span> just in an <span style=\"font-family: 'courier new', courier;\">if<\/span> or <span style=\"font-family: 'courier new', courier;\">switch<\/span> statement: <a href=\"https:\/\/www.modernescpp.com\/index.php\/cpp17-core\">C++17 &#8211; What&#8217;s new in the language?<\/a><\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-stack\">ES.27: Use&nbsp;<code class=\"highlighter-rouge no-highlight\">std::array<\/code>&nbsp;or&nbsp;<code class=\"highlighter-rouge no-highlight\">stack_array<\/code>&nbsp;for arrays on the stack<\/a><\/h3>\n<p>10 years ago, I thought that creating a variable-length array on the stack is ISO C++.&nbsp;<\/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;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> m <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">9<\/span>;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f<\/span>()\r\n{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> a1[n];\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> a2[m];   <span style=\"color: #0099ff; font-style: italic;\">\/\/ error: not ISO C++<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Wrong!&nbsp;<\/p>\n<p>In the first case, you should use a <span style=\"font-family: 'courier new', courier;\">std::array<\/span>; in the second case, you can use a <span style=\"font-family: 'courier new', courier;\">gsl::stack_array<\/span> from the <a href=\"https:\/\/github.com\/Microsoft\/GSL\">Guideline support library (GSL)<\/a>.&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;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> m <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">9<\/span>;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f<\/span>()\r\n{\r\n    std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, n<span style=\"color: #555555;\">&gt;<\/span> b1;\r\n    gsl<span style=\"color: #555555;\">::<\/span>stack_array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> b2(m);\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Why should you use <span style=\"font-family: 'courier new', courier;\">std::array<\/span>&nbsp;instead of C-array&nbsp;or <span style=\"font-family: 'courier new', courier;\">gsl::array<\/span>&nbsp;instead of C-array?<\/p>\n<p><span style=\"font-family: 'courier new', courier;\">std::array<\/span> knows its length in contrast to the C-array&nbsp;and will not decay to a pointer as a function parameter.&nbsp;&nbsp;How easy is it to use the following function for copying arrays with the wrong length n:&nbsp;<\/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;\">copy_n<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> T<span style=\"color: #555555;\">*<\/span> p, T<span style=\"color: #555555;\">*<\/span> q, <span style=\"color: #007788; font-weight: bold;\">int<\/span> n);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ copy from [p:p+n) to [q:q+n)<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;Variable-length arrays such as <span style=\"font-family: 'courier new', courier;\">int a2[m]<\/span> are a security risk because you may execute arbitrary code or get stack exhaustion.&nbsp;<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-lambda-init\">ES.28: Use lambdas for complex initialization, especially of&nbsp;<code class=\"highlighter-rouge no-highlight\">const<\/code>&nbsp;variables<\/a><\/h3>\n<p>In my seminars, I sometimes hear the question: Why should I invoke a lambda function just in place? This rule answers. You can put complex initialization in it. This in-place invocation is very valuable if your variable should become const.&nbsp;<\/p>\n<p>If you don&#8217;t want to modify your variable after the initialization, make it const according to the previous rule R.25. Fine. But sometimes, the variable&#8217;s initialization consists of more steps; therefore, you can make it not const.<\/p>\n<p>Have a look here. The widget <span style=\"font-family: 'courier new', courier;\">x<\/span> in the following example should be <span style=\"font-family: 'courier new', courier;\">const<\/span>&nbsp;after its initialization.&nbsp; It cannot be <span style=\"font-family: 'courier new', courier;\">const<\/span> because it will be changed a few times during its initialization.<\/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%;\">widget x;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ should be const, but:<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>; i <span style=\"color: #555555;\">&lt;=<\/span> N; <span style=\"color: #555555;\">++<\/span>i) {             <span style=\"color: #0099ff; font-style: italic;\">\/\/ this could be some<\/span>\r\n    x <span style=\"color: #555555;\">+=<\/span> some_obj.do_something_with(i);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ arbitrarily long code<\/span>\r\n}                                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ needed to initialize x<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ from here, x should be const, but we can't say so in code in this style<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Now, a lambda function comes to our rescue. Put the initialization stuff into a lambda function, capture the environment by reference, and initialize your const variable with the in-place invoked lambda function.<\/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;\">const<\/span> widget x <span style=\"color: #555555;\">=<\/span> [<span style=\"color: #555555;\">&amp;<\/span>]{\r\n    widget val;                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ widget has a default constructor<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>; i <span style=\"color: #555555;\">&lt;=<\/span> N; <span style=\"color: #555555;\">++<\/span>i) {            <span style=\"color: #0099ff; font-style: italic;\">\/\/ this could be some<\/span>\r\n        val <span style=\"color: #555555;\">+=<\/span> some_obj.do_something_with(i);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ arbitrarily long code<\/span>\r\n    }                                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ needed to initialize x<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> val;\r\n}();\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Admittedly, invoking a lambda function just in place looks strange, but I like it from the conceptual view. You put the whole initialization stuff just in a function body.&nbsp;<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-macros\">ES.30<\/a>,&nbsp;<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-macros2\">ES.31,&nbsp;&nbsp;<\/a><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-ALL_CAPS\">ES.32<\/a>&nbsp;and&nbsp;<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-MACROS\">ES.33<\/a><\/h3>\n<p>I will only paraphrase the following four rules to macros. Don&#8217;t use macros for program test manipulation or constants and functions. If you have to use them, use unique names with ALL_CAPS.<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-ellipses\">ES.34: Don\u2019t define a (C-style) variadic function<\/a><\/h3>\n<p>Right! Don&#8217;t define a (C-style) variadic function. Since C++11, we have <span style=\"font-family: 'courier new', courier;\">variadic templates;<\/span> since C++17, we have <a href=\"https:\/\/www.modernescpp.com\/index.php\/fold-expressions\">fold xpressions<\/a>. This is all that we need.&nbsp;<\/p>\n<p>You probably quite often used the (C-style) variadic function: <span style=\"font-family: 'courier new', courier;\">printf. printf<\/span>&nbsp;accepts a format string and arbitrary numbers of arguments and displays its arguments respectively. A call of print has undefined behavior if you don&#8217;t use the correct format specifiers or the number of your arguments isn&#8217;t correct.&nbsp;<\/p>\n<p>By using variadic templates, you can implement a type-safe printf function. Here is the simplified version of <span style=\"font-family: 'courier new', courier;\">printf<\/span> based on&nbsp;<a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/parameter_pack\">cppreference.com<\/a>.&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;\">\/\/ myPrintf.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">myPrintf<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> format){                         <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> format;\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> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span>... Targs<span style=\"color: #555555;\">&gt;<\/span>                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> myPrintf(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> format, T value, Targs... Fargs) \r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> ( ; <span style=\"color: #555555;\">*<\/span>format <span style=\"color: #555555;\">!=<\/span> <span style=\"color: #cc3300;\">'\\0'<\/span>; format<span style=\"color: #555555;\">++<\/span> ) {\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( <span style=\"color: #555555;\">*<\/span>format <span style=\"color: #555555;\">==<\/span> <span style=\"color: #cc3300;\">'%'<\/span> ) {\r\n           std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> value;                             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n           myPrintf(format<span style=\"color: #555555;\">+<\/span><span style=\"color: #ff6600;\">1<\/span>, Fargs...);                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n           <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n        }\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">*<\/span>format;\r\n    }\r\n}\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n    myPrintf(<span style=\"color: #cc3300;\">\"% world% %<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>,<span style=\"color: #cc3300;\">\"Hello\"<\/span>,<span style=\"color: #cc3300;\">'!'<\/span>,<span style=\"color: #ff6600;\">123<\/span>);            <span style=\"color: #0099ff; font-style: italic;\">\/\/ Hello world! 123<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: 'courier new', courier;\">myPrintf<\/span> can accept an arbitrary number of arguments. If arbitrary means 0, the first overload (1) is used. If arbitrary means more than 0, the second overload (2) is used. The function template (2) is quite interesting. It can accept an arbitrary number of arguments, but the number must exceed 0. The first argument will be bound to <span style=\"font-family: 'courier new', courier;\">value<\/span> and written to<span style=\"font-family: 'courier new', courier;\"> std::cout<\/span> (3). The rest of the arguments will be used in (4) to make a recursive call. This recursive call will create another function template <span style=\"font-family: 'courier new', courier;\">myPrintf<\/span>, accepting one argument less. This recursion will go to zero. In this case, the function <span style=\"font-family: 'courier new', courier;\">myPrintf<\/span> (1) as boundary condition kicks in.&nbsp;<\/p>\n<p><span style=\"font-family: 'courier new', courier;\">myPrintf<\/span> is type-safe because all output will be handled by std::cout. This simplified implementation cannot handle format strings such as&nbsp; <span style=\"font-family: 'courier new', courier;\">%d, %f, <\/span>or<span style=\"font-family: 'courier new', courier;\"> 5.5f.<\/span><span class=\"c1\"><br \/><\/span><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>There is a lot to write about expression. The C++ core guidelines have about 25 rules; therefore, my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-expressions\">next post<\/a> will deal with expression.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I will finish the rules for declarations. The remaining rules for declarations are not especially sophisticated but important for high code quality.<\/p>\n","protected":false},"author":21,"featured_media":5373,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[497],"class_list":["post-5374","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-declarations"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5374","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=5374"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5374\/revisions"}],"predecessor-version":[{"id":6843,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5374\/revisions\/6843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5373"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}