{"id":5779,"date":"2019-09-18T15:23:22","date_gmt":"2019-09-18T15:23:22","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-type-safety-per-design\/"},"modified":"2023-08-11T16:04:00","modified_gmt":"2023-08-11T16:04:00","slug":"c-core-guidelines-type-safety-per-design","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-type-safety-per-design\/","title":{"rendered":"C++ Core Guidelines: Type Safety by Design"},"content":{"rendered":"<p>What does that mean: type safety by design. Type safety by design just means that you always initialize your variables, use <span style=\"font-family: courier new, courier;\">std:<\/span>:variant instead of a union, or prefer variadic templates and fold expressions to <span style=\"font-family: courier new, courier;\">va_arg<\/span>&#8216;s.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5776\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/abseiling-1842180_1280.jpg\" alt=\"abseiling 1842180 1280\" width=\"400\" height=\"267\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/abseiling-1842180_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/abseiling-1842180_1280-300x200.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/abseiling-1842180_1280-1024x682.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/abseiling-1842180_1280-768x512.jpg 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>As in my first post to type safety<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-profiles-type-safety-bounds-safety-and-lifetime-safety\"> C++ Core Guidelines: Type Safety<\/a>, I will name the four missing types of type safety and add additional information, if necessary. Here we are:<\/p>\n<h2 id=\"h1-type-safety\"><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#SS-type\">Type Safety<\/a><\/h2>\n<h3>Type.5: Don\u2019t use a variable before it has been initialized<\/h3>\n<p>The rules on which object will be initialized are quite difficult to get in C++. Here is a simple example.<\/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;\">struct<\/span> T1 {};\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T2<\/span>{\n <span style=\"color: #9999ff;\">public:<\/span>\n    T2() {} \n};\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> n;                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> n2;              <span style=\"color: #0099ff; font-style: italic;\">\/\/ ERROR<\/span>\n  std<span style=\"color: #555555;\">::<\/span>string s;       <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\n  T1 t1;               <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\n  T2 t2;               <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK                  <\/span>\n}\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">n<\/span> is a global variable; therefore, it is initialized to 0. This initialization will not hold for<span style=\"font-family: courier new, courier;\"> n2<\/span> because it is a local variable and is, therefore, not initialized. But they are initialized if you use a user-defined type such as std::string, T1, or T2 in a global or local scope.<\/p>\n<p>If that is too difficult for you, I have a simple fix. Use <span style=\"font-family: 'courier new', courier;\">auto.<\/span> The c ompiler can not guess from an expression <span style=\"font-family: courier new, courier;\">auto a<\/span> what type <span style=\"font-family: courier new, courier;\">a<\/span> has to be. Now, you can not forget to initialize the variable. You are forced to initialize your variables.<\/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;\">struct<\/span> T1 {};\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T2<\/span>{\n <span style=\"color: #9999ff;\">public:<\/span>\n    T2() {}\n};\n\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> n2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> s <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"\"<\/span>s;      \n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> t1 <span style=\"color: #555555;\">=<\/span> T1();               \n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> t2 <span style=\"color: #555555;\">=<\/span> T2();\n}\n<\/pre>\n<\/div>\n<h3>Type.6: Always initialize a member variable<\/h3>\n<p>In this case, I can make it short. I have already written in my post <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-more-non-rules-and-myths\">C++ Core Guidelines: More Non-Rules and Myths<\/a>, about the initialization of member variables.<\/p>\n<h3>Type.7: Avoid naked union<\/h3>\n<p>First of all: What is a union? A union is a user-defined type that can hold one of its members at a time.<\/p>\n<p>&#8220;Naked&#8221; unions are error-prone because you must keep track of the underlying type.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ nakedUnion.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">union<\/span> Value {\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> i;\n    <span style=\"color: #007788; font-weight: bold;\">double<\/span> d;\n};\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n  Value v;\n  v.d <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">987.654<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ v holds a double<\/span>\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"v.d: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> v.d <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;     \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"v.i: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> v.i <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n  v.i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">123<\/span>;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ v holds an int<\/span>\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"v.i: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> v.i <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"v.d: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> v.d <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n}\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;The union holds int the first iteration a <span style=\"font-family: courier new,courier;\">double<\/span> and in the second iteration an <span style=\"font-family: courier new,courier;\">int <\/span>value. You get undefined behavior if you read a double as an int (1) or an int as a double (2).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5346\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/11\/nakedUnion.png\" alt=\"nakedUnion\" width=\"300\" height=\"208\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/11\/nakedUnion.png 391w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/11\/nakedUnion-300x208.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h3><span style=\"font-family: courier new, courier;\">std::variant<\/span><\/h3>\n<p>A <span style=\"font-family: courier new,courier;\">std::variant<\/span> is, in contrast, a type-safe union. We have had it since C++17. An instance of <span style=\"font-family: courier new,courier;\">std::variant<\/span> has a value from one of its types. The type must not be a reference, array, or <span style=\"font-family: courier new,courier;\">void.<\/span> A default-initialized <span style=\"font-family: courier new,courier;\">std::variant<\/span> will be initialized with its first type. In this case, the first type must have a default constructor. Here is a simple example based on <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/utility\/variant\">cppreference.com.<\/a><\/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;\">\/\/ variant.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;variant&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n \n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n\n  std<span style=\"color: #555555;\">::<\/span>variant<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">float<\/span><span style=\"color: #555555;\">&gt;<\/span> v, w;       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n  v <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">12<\/span>;                    <span style=\"color: #0099ff; font-style: italic;\"><\/span>\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>get<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(v);\n  w <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>get<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(v);                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n  w <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>get<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">0<\/span><span style=\"color: #555555;\">&gt;<\/span>(v);                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span><span style=\"color: #0099ff; font-style: italic;\"><\/span>\n  w <span style=\"color: #555555;\">=<\/span> v;                     <span style=\"color: #0099ff; font-style: italic;\">          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span><\/span>\n \n  <span style=\"color: #0099ff; font-style: italic;\">\/\/  std::get&lt;double&gt;(v);   \/\/ error: no double in [int, float] (3)<\/span>\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/  std::get&lt;3&gt;(v);        \/\/ error: valid index values are 0 and 1 (4)<\/span>\n \n  try{\n    std<span style=\"color: #555555;\">::<\/span>get<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">float<\/span><span style=\"color: #555555;\">&gt;<\/span>(w);                <span style=\"color: #0099ff; font-style: italic;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span><\/span><span style=\"color: #0099ff; font-style: italic;\">&nbsp;  <\/span>\n  }\n  <span style=\"color: #006699; font-weight: bold;\">catch<\/span> (std<span style=\"color: #555555;\">::<\/span>bad_variant_access<span style=\"color: #555555;\">&amp;<\/span>) {}\n \n  std<span style=\"color: #555555;\">::<\/span>variant<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> v2(<span style=\"color: #cc3300;\">\"abc\"<\/span>); <span style=\"color: #0099ff; font-style: italic;\"><span style=\"color: #0099ff; font-style: italic;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span><\/span><span style=\"color: #0099ff; font-style: italic;\"><\/span><\/span>\n  v2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"def\"<\/span>;                   <span style=\"color: #0099ff; font-style: italic;\">&nbsp;      <span style=\"color: #0099ff; font-style: italic;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ (7)<\/span><\/span><span style=\"color: #0099ff; font-style: italic;\"><\/span><\/span>\n\n}\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I define in line (1) both variants <span style=\"font-family: courier new,courier;\">v<\/span> and<span style=\"font-family: courier new,courier;\"> w<\/span>. Both can have an <span style=\"font-family: courier new,courier;\">int<\/span> and a <span style=\"font-family: courier new,courier;\">float<\/span> value. <span style=\"font-family: courier new,courier;\"><\/span> <span style=\"font-family: courier new,courier;\">std::get&lt;int&gt;(v)<\/span> returns the value. In line (2), you see three possibilities to assign the variant <span style=\"font-family: courier new,courier;\">v<\/span> to the variant <span style=\"font-family: courier new,courier;\">w.<\/span> But you have to keep a few rules in mind. You can ask for the value of a variant by type (line 3) or index (line 4). The type must be unique and the index valid. On line (5), the variant <span style=\"font-family: courier new,courier;\">w<\/span> holds an <span style=\"font-family: courier new,courier;\">int<\/span> value. Therefore, I get a <span style=\"font-family: courier new,courier;\">std::bad_variant_access<\/span> exception. A conversion can occur if the constructor call or assignment call is unambiguous. That is the reason that it&#8217;s possible to construct a <span style=\"font-family: courier new,courier;\">std::variant&lt;std::string&gt;<\/span> in line (6) with a C-string or assign a new C-string to the variant (line 7).<\/p>\n<h3>Type.8: Avoid varargs<\/h3>\n<p>Variadic functions include <span style=\"font-family: courier new, courier;\">std::printf<\/span> which can take an arbitrary number of arguments. The issue is that you must assume that the correct types were passed. Of course, this assumption is very error-prone and relies on the programmer&#8217;s discipline.&nbsp;<\/p>\n<p>Here is a small example to understand the implicit danger of variadic functions.<\/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;\">\/\/ vararg.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cstdarg&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">sum<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> num, ... ){\n  \n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> sum{};\n      \n    <span style=\"color: #007788; font-weight: bold;\">va_list<\/span> argPointer;\n    va_start(argPointer, num );\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> num; i<span style=\"color: #555555;\">++<\/span> )\n        sum <span style=\"color: #555555;\">+=<\/span> va_arg(argPointer, <span style=\"color: #007788; font-weight: bold;\">int<\/span> );\n    va_end(argPointer);\n      \n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> sum;\n}\n \n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum(1, 5): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum(3, 1, 2, 3): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum(<span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum(3, 1, 2, 3, 4): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum(<span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>)  <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum(3, 1, 2, 3.5): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum(<span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3.5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n\n}\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">sum<\/span> is a variadic function. Its first argument is the number of arguments that should be summed up. I will only provide so much info to the varargs macros so you can understand the program. For more information, read <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/variadic\">cppreference.com<\/a>.<\/p>\n<ul>\n<li><strong><span style=\"font-family: courier new, courier;\">va_list<\/span>: <\/strong>holds the necessary information for the following macros<\/li>\n<li><strong><span style=\"font-family: courier new, courier;\">va_star<\/span><\/strong>t: enables access to the variadic function arguments<\/li>\n<li><strong><span style=\"font-family: courier new, courier;\">va_arg:<\/span><\/strong> accesses the next variadic function argument<\/li>\n<li><strong><span style=\"font-family: courier new, courier;\">va_end<\/span><\/strong>: end the access of the variadic function arguments<\/li>\n<\/ul>\n<p>In line (1) and line (2), I had a bad day. First, the number of arguments is wrong; second, I provided a <span style=\"font-family: courier new, courier;\">double<\/span> instead of an <span style=\"font-family: courier new, courier;\">int<\/span>. The output shows both issues. The last element in line (1) is missing, and the <span style=\"font-family: courier new, courier;\">double<\/span> is interpreted as <span style=\"font-family: courier new, courier;\">int <\/span>(line 2).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5777\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/vararg.png\" alt=\"vararg\" width=\"250\" height=\"167\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/vararg.png 378w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/vararg-300x200.png 300w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/p>\n<p>This issue can be easily overcome with fold expressions in C++17:<\/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;\">\/\/ foldExpressions.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #aa0000; background-color: #ffaaaa;\">...<\/span><span style=\"color: #00aa88; font-weight: bold;\">Args<\/span><span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> sum(Args... args) { \n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> (... <span style=\"color: #555555;\">+<\/span> args); \n}\n \n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum(5): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum(<span style=\"color: #ff6600;\">5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum(1, 2, 3): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum(1, 2, 3, 4): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>)  <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum(1, 2, 3.5): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3.5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;    \n\n}\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Okay, the function <span style=\"font-family: courier new, courier;\">sum<\/span> may look terrifying to you. C++11 supports variadic templates. These are templates that can accept an arbitrary number of arguments. A parameter pack holds the arbitrary number denote by an ellipse (&#8230;). Additionally, with C++17, you can directly reduce a parameter pack with a binary operator. This addition, based on variadic templates, is called fold expressions. In the case of the sum function, the binary + operator (<span style=\"font-family: courier new, courier;\">&#8230;+ args<\/span>) is applied. If you want to know more about fold expressions in C++17, here is my<a href=\"https:\/\/www.modernescpp.com\/index.php\/fold-expressions\"> previous post<\/a> on it.&nbsp;<\/p>\n<p>The output of the program is as expected:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5136\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/foldExpressions.png\" alt=\"foldExpressions\" width=\"350\" height=\"174\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>Additionally to variadic templates and fold expression, there is another comfortable way for a function to accept an arbitrary number of arguments of a specific type: use a container of the STL such as<span style=\"font-family: courier new, courier;\"> std::vector<\/span> <span style=\"font-family: courier new, courier;\"><\/span>as an argument.<\/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;\">\/\/ vectorSum.cpp<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span><br \/>\n<span style=\"color: #009999;\">#include &lt;numeric&gt;<\/span><br \/>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span><\/p>\n<p><span style=\"color: #006699; font-weight: bold;\">auto<\/span> <span style=\"color: #cc00ff;\">sum<\/span>(std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myVec){<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>accumulate(myVec.begin(), myVec.end(), <span style=\"color: #ff6600;\">0<\/span>);<br \/>\n}<\/p>\n<p><span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){<\/p>\n<p>std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">&#8220;sum({5}): &#8220;<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum({<span style=\"color: #ff6600;\">5<\/span>}) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;<br \/>\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">&#8220;sum({1, 2, 3}): &#8220;<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum({<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>}) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;<br \/>\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">&#8220;sum({1, 2, 3, 4}): &#8220;<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum({<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>}) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;<\/p>\n<p>}<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In this case, a <span style=\"font-family: courier new, courier;\">std::initializer_list&lt;int&gt;<\/span> is used as an argument of the function <span style=\"font-family: courier new, courier;\">sum<\/span>. A <span style=\"font-family: courier new, courier;\">std::initializer_list<\/span> can directly initialize a<span style=\"font-family: courier new, courier;\"> std::vector<\/span>. In contrast, to fold expressions, <span style=\"font-family: courier new, courier;\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/accumulate\">std::accumulat<\/a>e<\/span> is performed at runtime.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5778\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/vectorSum.png\" alt=\"vectorSum\" width=\"350\" height=\"153\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/vectorSum.png 543w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/vectorSum-300x131.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p><a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-bounds-safety\">Next time<\/a>, I will continue with the profile to Bounds Safety. This profile has four rules.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What does that mean: type safety by design. Type safety by design just means that you always initialize your variables, use std::variant instead of a union, or prefer variadic templates and fold expressions to va_arg&#8216;s.<\/p>\n","protected":false},"author":21,"featured_media":5776,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[466],"class_list":["post-5779","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-safety"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5779","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=5779"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5779\/revisions"}],"predecessor-version":[{"id":8047,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5779\/revisions\/8047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5776"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}