{"id":5299,"date":"2017-08-04T13:45:06","date_gmt":"2017-08-04T13:45:06","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-how-to-pass-function-parameters\/"},"modified":"2023-06-26T12:08:58","modified_gmt":"2023-06-26T12:08:58","slug":"c-core-guidelines-how-to-pass-function-parameters","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-how-to-pass-function-parameters\/","title":{"rendered":"C++ Core Guidelines: The Rules for in, out, in-out, consume, and forward Function Parameter"},"content":{"rendered":"<p>You have many choices to pass function parameters. You can pass by value or by reference. A reference can be const or non-const. You can even move or forward your parameters.&nbsp; Your decision should depend on if it is an <strong>in<\/strong>, and<strong> out<\/strong>, an <strong>in-out<\/strong>, a <strong>consume<\/strong>, or a <strong>forward<\/strong> function parameter. Curious? Read the post!<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>According to the guidelines, let&#8217;s discuss <strong>in<\/strong>, <strong>out<\/strong>, <strong>in-out<\/strong>, <strong>consume<\/strong>, or <strong>forward<\/strong> parameters.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Parameter_passing_expression_rules\"><\/span>Parameter passing expression rules:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-conventional\">F.15: Prefer simple and conventional ways of passing information<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-in\">F.16: For \u201cin\u201d parameters, pass cheaply-copied types by value and others by reference to <code class=\"highlighter-rouge no-highlight\">const<\/code><\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-inout\">F.17: For \u201cin-out\u201d parameters, pass by reference to non-<code class=\"highlighter-rouge no-highlight\">const<\/code><\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-consume\">F.18: For \u201cconsume\u201d parameters, pass by <code class=\"highlighter-rouge no-highlight\">X&amp;&amp;<\/code> and <code class=\"highlighter-rouge no-highlight\">std::move<\/code> the parameter<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-forward\">F.19: For \u201cforward\u201d parameters, pass by <code class=\"highlighter-rouge no-highlight\">TP&amp;&amp;<\/code> and only <code class=\"highlighter-rouge no-highlight\">std::forward<\/code> the parameter<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-out\">F.20: For \u201cout\u201d output values, prefer return values to output parameters<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-out-multi\">F.21: To return multiple \u201cout\u201d values, prefer returning a tuple or struct<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-ptr-ref\">F.60: Prefer <code class=\"highlighter-rouge no-highlight\">T*<\/code> over <code class=\"highlighter-rouge no-highlight\">T&amp;<\/code> when \u201cno argument\u201d is a valid option<\/a><\/li>\n<\/ul>\n<p>It seems to be a lot of stuff, but bear with me. The first rule F.15 summarises the guidelines F.16 &#8211; F.21<\/p>\n<h3><span class=\"ez-toc-section\" id=\"F15_Prefer_simple_and_conventional_ways_of_passing_information\"><\/span><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-conventional\">F.15: Prefer simple and conventional ways of passing information<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Here is the big picture from the C++ core guidelines. These are the standard parameter passing rules.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5297\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/FunctionParameters.png\" alt=\"FunctionParameters\" width=\"600\" height=\"196\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/FunctionParameters.png 784w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/FunctionParameters-300x98.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/FunctionParameters-768x250.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Based on these rules there are a few additions in green, the so-called advanced parameter passing rules.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5298\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/FunctionParametersAdvanced.png\" alt=\"FunctionParametersAdvanced\" width=\"600\" height=\"209\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/FunctionParametersAdvanced.png 792w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/FunctionParametersAdvanced-300x104.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/FunctionParametersAdvanced-768x267.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The rationale for the rules and their variations will follow in the next rules.<\/p>\n<\/p>\n<h3><span class=\"ez-toc-section\" id=\"F16_For_%E2%80%9Cin%E2%80%9D_parameters_pass_cheaply-copied_types_by_value_and_others_by_reference_to_const\"><\/span><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-in\">F.16: For \u201cin\u201d parameters, pass cheaply-copied types by value and others by reference to <code class=\"highlighter-rouge no-highlight\">const<\/code><\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This rule for <strong>in<\/strong> parameters is straightforward, and so is the 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: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f1<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> string<span style=\"color: #555555;\">&amp;<\/span> s);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK: pass by reference to const; always cheap<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f2<\/span>(string s);         <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: potentially expensive<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f3<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> x);            <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK: Unbeatable<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f4<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;<\/span> x);     <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: overhead on access in f4()<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I often hear the question in my seminars: What is cheaply copyable? The guidelines are pretty concrete.<\/p>\n<ul>\n<li>You should not&nbsp; copy the parameter <span style=\"font-family: courier new,courier;\">p<\/span> if <span style=\"font-family: courier new,courier;\">sizeof(p) &gt; 4 * sizeof(int)<\/span><\/li>\n<li>You should not use a const reference to <span style=\"font-family: courier new,courier;\">p<\/span> if <span style=\"font-family: courier new,courier;\">sizeof(p) &lt; 3 * sizeof(int)<\/span><\/li>\n<\/ul>\n<p>&nbsp;I assume these numbers are based on experience.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"F17_For_%E2%80%9Cin-out%E2%80%9D_parameters_pass_by_reference_to_non-const\"><\/span><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-inout\">F.17: For \u201cin-out\u201d parameters, pass by reference to non-<code class=\"highlighter-rouge no-highlight\">const<\/code><\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>In-out<\/strong> parameters will be modified in the function, so using a non-const reference makes sense.<\/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;\">appendElements<\/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;&amp;<\/span> vec){\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ append elements to vec<\/span>\r\n  ...\r\n}\r\n<\/pre>\n<\/div>\n<h3><span class=\"ez-toc-section\" id=\"F18_For_%E2%80%9Cconsume%E2%80%9D_parameters_pass_by_X_and_std_move_the_parameter\"><\/span><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-consume\">F.18: For \u201cconsume\u201d parameters, pass by <code class=\"highlighter-rouge no-highlight\">X&amp;&amp;<\/code> and <code class=\"highlighter-rouge no-highlight\">std::move<\/code> the parameter<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This is the first advanced rule to <strong>consume<\/strong> parameters. Use an rvalue reference if you consume the parameter and move it inside the function body. Here is an 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: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">sink<\/span>(vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&amp;&amp;<\/span> v) {   <span style=\"color: #0099ff; font-style: italic;\">\/\/ sink takes ownership of whatever the argument owned<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ usually there might be const accesses of v here<\/span>\r\n    store_somewhere(std<span style=\"color: #555555;\">::<\/span>move(v));\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ usually no more use of v here; it is moved-from<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>There is an exception to this rule. <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> is a move-only type that is cheap to move. Therefore, you can move it.<\/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;\">sink<\/span>(std::unique_ptr<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> p) { <br \/>  ...<span style=\"color: #0099ff; font-style: italic;\"><\/span><span style=\"color: #0099ff; font-style: italic;\"><\/span><span style=\"color: #555555;\"><\/span><span style=\"color: #0099ff; font-style: italic;\"><\/span>\r\n}<br \/>...<br \/>sink(std::move(uniqPtr));\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"F19_For_%E2%80%9Cforward%E2%80%9D_parameters_pass_by_TP_and_only_std_forward_the_parameter\"><\/span><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-forward\">F.19: For \u201cforward\u201d parameters, pass by <code class=\"highlighter-rouge no-highlight\">TP&amp;&amp;<\/code> and only <code class=\"highlighter-rouge no-highlight\">std::forward<\/code> the parameter<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This is the idiom that factory methods such as <span style=\"font-family: courier new,courier;\">std::make_unique<\/span> or <span style=\"font-family: courier new,courier;\">std::make_shared <\/span>use. Both functions take a type <span style=\"font-family: courier new,courier;\">T<\/span> and arbitrary numbers of arguments<span style=\"font-family: courier new,courier;\"> args<\/span> and <strong>forward<\/strong> them <strong>unchanged<\/strong> to the constructor of <span style=\"font-family: courier new,courier;\">T<\/span>. Have a look here:<\/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;\">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>... Args<span style=\"color: #555555;\">&gt;                              \/\/ 1<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> make_unique(Args<span style=\"color: #555555;\">&amp;&amp;<\/span>... args)                      \/\/ 2\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #006699; font-weight: bold;\">new<\/span> T(std<span style=\"color: #555555;\">::<\/span>forward<span style=\"color: #555555;\">&lt;<\/span>Args<span style=\"color: #555555;\">&gt;<\/span>(args)...));  \/\/ 3\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This pattern is called perfect forwarding: If a function templates forward its arguments without changing its lvalue or rvalue characteristics, we call it perfect forwarding.&nbsp;<\/p>\n<p>Here is my previous post to<a href=\"https:\/\/www.modernescpp.com\/index.php\/perfect-forwarding\"> perfect forwarding. <\/a><\/p>\n<p>To get perfect forwarding for a function template, you have to follow the three-step recipe. I skip this part because it must not be a variadic template (&#8230;) such as for std::make_unique.<\/p>\n<ol>\n<li>You need a template parameter:&nbsp;<span style=\"font-family: courier new,courier;\">typename Args<\/span><\/li>\n<li>Take your function argument per forwarding reference: <span style=\"font-family: courier new,courier;\">Args&amp;&amp; args<\/span><\/li>\n<li>Forward the function arguments: <span style=\"font-family: courier new,courier;\">std::forward&lt;Args&gt;(args)<\/span><\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"F20_For_%E2%80%9Cout%E2%80%9D_output_values_prefer_return_values_to_output_parameters\"><\/span><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-out\">F.20: For \u201cout\u201d output values, prefer return values to output parameters<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>An explicit return value documents the intention of a function. Using a parameter with a reference as<strong> out<\/strong> output value may be misleading. This can also be an in-out value. Returning the result of a function by value also holds for the standard container that uses move semantics implicitly.<\/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;\">\/\/ OK: return pointers to elements with the value x<\/span>\r\nvector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*&gt;<\/span> find_all(<span style=\"color: #006699; font-weight: bold;\">const<\/span> vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&amp;<\/span>, <span style=\"color: #007788; font-weight: bold;\">int<\/span> x);\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Bad: place pointers to elements with value x in-out<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">find_all<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&amp;<\/span>, vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*&gt;&amp;<\/span> out, <span style=\"color: #007788; font-weight: bold;\">int<\/span> x);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>There is an exception to this rule. If you have an expensive-to-move object, you can use a reference as out parameter.<\/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> Package {      <span style=\"color: #0099ff; font-style: italic;\">\/\/ exceptional case: expensive-to-move object<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">char<\/span> header[<span style=\"color: #ff6600;\">16<\/span>];\r\n    <span style=\"color: #007788; font-weight: bold;\">char<\/span> load[<span style=\"color: #ff6600;\">2024<\/span> <span style=\"color: #555555;\">-<\/span> <span style=\"color: #ff6600;\">16<\/span>];\r\n};\r\n\r\nPackage <span style=\"color: #cc00ff;\">fill<\/span>();       <span style=\"color: #0099ff; font-style: italic;\">\/\/ Bad: large return value<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">fill<\/span>(Package<span style=\"color: #555555;\">&amp;<\/span>);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"F21_To_return_multiple_%E2%80%9Cout%E2%80%9D_values_prefer_returning_a_tuple_or_struct\"><\/span><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-out-multi\">F.21: To return multiple \u201cout\u201d values prefer returning a tuple or struct<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Sometimes your function returns more than one <strong>out<\/strong> value. In this case, you should use a <span style=\"font-family: courier new,courier;\">std::tuple<\/span> or a struct, but you should not use the parameter with a reference. This is very error-prone.<\/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;\">\/\/ BAD: output-only parameter documented in a comment<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">f<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> string<span style=\"color: #555555;\">&amp;<\/span> input, <span style=\"color: #0099ff; font-style: italic;\">\/*output only*\/<\/span> string<span style=\"color: #555555;\">&amp;<\/span> output_data)\r\n{\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n    output_data <span style=\"color: #555555;\">=<\/span> something();\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> status;\r\n}\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ GOOD: self-documenting<\/span>\r\ntuple<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, string<span style=\"color: #555555;\">&gt;<\/span> f(<span style=\"color: #006699; font-weight: bold;\">const<\/span> string<span style=\"color: #555555;\">&amp;<\/span> input)\r\n{\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> make_tuple(status, something());\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>With C++17 and <a href=\"https:\/\/www.modernescpp.com\/index.php\/cpp17-core\">structured binding <\/a>returning more than one value becomes quite convenient.<\/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;\">auto<\/span> [value, success] <span style=\"color: #555555;\">=<\/span> getValue(key);\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (success){\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ do something with the value;<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The function <span style=\"font-family: courier new,courier;\">getValue<\/span> returns a pair. success indicates if the query for the key was successful.<\/p>\n<p>The next rule is special. For me, this rule is more of a semantic rule. But anyway.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"F60_Prefer_T_over_T_when_%E2%80%9Cno_argument%E2%80%9D_is_a_valid_option\"><\/span><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-ptr-ref\">F.60: Prefer <code class=\"highlighter-rouge no-highlight\">T*<\/code> over <code class=\"highlighter-rouge no-highlight\">T&amp;<\/code> when \u201cno argument\u201d is a valid option<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If your parameter can never get a &#8220;no argument&#8221; such as a<span style=\"font-family: courier new,courier;\"> nullptr<\/span>, you should use a <span style=\"font-family: courier new,courier;\">T&amp;<\/span>.&nbsp;<span style=\"font-family: courier new,courier;\">T&amp; <\/span>cannot be a<span style=\"font-family: courier new,courier;\"> nullptr.<\/span> If <span style=\"font-family: courier new,courier;\">nullptr<\/span> is possible, use <span style=\"font-family: courier new,courier;\">T*.<\/span><span style=\"font-family: courier new,courier;\"><br \/><\/span><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std<span style=\"color: #555555;\">::<\/span>string upperString(std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">*<\/span> str){\r\n  <span style=\"color: #006699; font-weight: bold;\">if<\/span> (str <span style=\"color: #555555;\">==<\/span> nullptr) <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>string{};  <span style=\"color: #0099ff; font-style: italic;\">\/\/ check for nullptr<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">else<\/span>{\r\n    ...\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>If no argument is an option, you have to check for it.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Whats_next\"><\/span>What&#8217;s next<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This post was about&nbsp;<strong>in<\/strong>, <strong>out<\/strong>, <strong>in-out<\/strong>, <strong>consume<\/strong>, and <strong>forward<\/strong> parameters, but there are more questions to answer. How should you deal with sequences or with ownership? I will write about it in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-semantic-of-function-parameter-and-return-values\">next post<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You have many choices to pass function parameters. You can pass by value or by reference. A reference can be const or non-const. You can even move or forward your parameters.&nbsp; Your decision should depend on if it is an in, and out, an in-out, a consume, or a forward function parameter. Curious? Read the [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":5297,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[503],"class_list":["post-5299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-functions"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5299","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=5299"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5299\/revisions"}],"predecessor-version":[{"id":6866,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5299\/revisions\/6866"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5297"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}