{"id":18379,"date":"2025-02-18T11:03:56","date_gmt":"2025-02-18T11:03:56","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=18379"},"modified":"2025-02-20T13:19:34","modified_gmt":"2025-02-20T13:19:34","slug":"ways-to-pass-values-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=18379","title":{"rendered":"How to pass arguments (C++)"},"content":{"rendered":"<p><strong>Analysis<\/strong><\/p>\n<details>\n<summary>Code \u25bc<\/summary>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n\r\nclass Foo {\r\npublic:\r\n    std::string bar;\r\n\r\n    Foo(const std::string&amp; d) : bar(d) {\r\n        std::cout &lt;&lt; &quot;Constructor: &quot; &lt;&lt; bar &lt;&lt; &quot;\\n&quot;;\r\n    }\r\n\r\n    Foo(const Foo&amp; other) : bar(other.bar) {\r\n        std::cout &lt;&lt; &quot;Copy Constructor: &quot; &lt;&lt; bar &lt;&lt; &quot;\\n&quot;;\r\n    }\r\n\r\n    Foo(Foo&amp;&amp; other) noexcept : bar(std::move(other.bar)) {\r\n        std::cout &lt;&lt; &quot;Move Constructor: &quot; &lt;&lt; bar &lt;&lt; &quot;\\n&quot;;\r\n    }\r\n\r\n    Foo&amp; operator=(const Foo&amp; other) {\r\n        std::cout &lt;&lt; &quot;Copy Assignment: &quot; &lt;&lt; other.bar &lt;&lt; &quot;\\n&quot;;\r\n        bar = other.bar;\r\n        return *this;\r\n    }\r\n\r\n    Foo&amp; operator=(Foo&amp;&amp; other) noexcept {\r\n        std::cout &lt;&lt; &quot;Move Assignment: &quot; &lt;&lt; other.bar &lt;&lt; &quot;\\n&quot;;\r\n        bar = std::move(other.bar);\r\n        return *this;\r\n    }\r\n\r\n    void print() const {\r\n        \/\/ std::cout &lt;&lt; &quot;Current content: &quot; &lt;&lt; bar &lt;&lt; &quot;\\n&quot;;\r\n    }\r\n};\r\n\r\nFoo g_foo(&quot;bar_0&quot;);\r\n\r\nvoid copy_no_store(Foo foo) { foo.print(); }\r\nvoid const_ref_no_store(const Foo&amp; foo) { foo.print(); }\r\nvoid ref_no_store(Foo&amp; foo) { foo.print(); }\r\ntemplate &lt;typename T&gt; void uni_ref_no_store(T&amp;&amp; foo) { foo.print(); }\r\ntemplate &lt;typename T&gt; void const_uni_ref_no_store(const T&amp;&amp; foo) { foo.print(); }\r\n\r\nvoid copy_store(Foo foo) { g_foo = std::move(foo); }\r\nvoid const_ref_store(const Foo&amp; foo) { g_foo = foo; }\r\nvoid ref_store(Foo&amp; foo) { g_foo = foo; }\r\ntemplate &lt;typename T&gt; void uni_ref_store(T&amp;&amp; foo) { g_foo = std::forward&lt;T&gt;(foo); }\r\ntemplate &lt;typename T&gt; void const_uni_ref_store(const T&amp;&amp; foo) { g_foo = std::forward&lt;const T&gt;(foo); }\r\n\r\nvoid run_copy_no_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_1&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    copy_no_store(foo);\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    copy_no_store(std::move(foo));\r\n}\r\nvoid run_const_ref_no_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_2&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    const_ref_no_store(foo);\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    const_ref_no_store(std::move(foo));\r\n}\r\nvoid run_ref_no_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_3&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    ref_no_store(foo);\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    \/\/ref_no_store(std::move(foo));\r\n    std::cout &lt;&lt; &quot;not possible\\n&quot;;\r\n}\r\nvoid run_uni_ref_no_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_4&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    uni_ref_no_store(foo);\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    uni_ref_no_store(std::move(foo));\r\n}\r\nvoid run_const_uni_ref_no_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_5&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    \/\/const_uni_ref_no_store(foo);\r\n    std::cout &lt;&lt; &quot;not possible\\n&quot;;\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    const_uni_ref_no_store(std::move(foo));\r\n}\r\n\r\nvoid run_copy_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_1&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    copy_store(foo);\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    copy_store(std::move(foo));\r\n}\r\nvoid run_const_ref_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_2&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    const_ref_store(foo);\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    const_ref_store(std::move(foo));\r\n}\r\nvoid run_ref_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_3&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    ref_store(foo);\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    \/\/ref_store(std::move(foo));\r\n    std::cout &lt;&lt; &quot;not possible\\n&quot;;\r\n}\r\nvoid run_uni_ref_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_4&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    uni_ref_store(foo);\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    uni_ref_store(std::move(foo));\r\n}\r\nvoid run_const_uni_ref_store() {\r\n    std::cout &lt;&lt; &quot;\\n&quot; &lt;&lt; __func__ &lt;&lt; &quot;\\n&quot;;\r\n    Foo foo(&quot;bar_5&quot;);\r\n    std::cout &lt;&lt; &quot;=== lvalue ===\\n&quot;;\r\n    \/\/const_uni_ref_store(foo);\r\n    std::cout &lt;&lt; &quot;not possible\\n&quot;;\r\n    std::cout &lt;&lt; &quot;=== rvalue ===\\n&quot;;\r\n    const_uni_ref_store(std::move(foo));\r\n}\r\n\r\nint main() {\r\n    std::cout &lt;&lt; &quot;\\nNot store copy\\n&quot;;\r\n    run_copy_no_store();\r\n    run_const_ref_no_store();\r\n    run_ref_no_store();\r\n    run_uni_ref_no_store();\r\n    run_const_uni_ref_no_store();\r\n\r\n    std::cout &lt;&lt; &quot;\\nStore copy\\n&quot;;\r\n    run_copy_store();\r\n    run_const_ref_store();\r\n    run_ref_store();\r\n    run_uni_ref_store();\r\n    run_const_uni_ref_store();\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<\/details>\n<details>\n<summary>Output \u25bc<\/summary>\n<pre>\r\nConstructor: bar_0\r\n\r\nNot store copy\r\n\r\nrun_copy_no_store\r\nConstructor: bar_1\r\n=== lvalue ===\r\nCopy Constructor: bar_1\r\n=== rvalue ===\r\nMove Constructor: bar_1\r\n\r\nrun_const_ref_no_store\r\nConstructor: bar_2\r\n=== lvalue ===\r\n=== rvalue ===\r\n\r\nrun_ref_no_store\r\nConstructor: bar_3\r\n=== lvalue ===\r\n=== rvalue ===\r\nnot possible\r\n\r\nrun_uni_ref_no_store\r\nConstructor: bar_4\r\n=== lvalue ===\r\n=== rvalue ===\r\n\r\nrun_const_uni_ref_no_store\r\nConstructor: bar_5\r\n=== lvalue ===\r\nnot possible\r\n=== rvalue ===\r\n\r\nStore copy\r\n\r\nrun_copy_store\r\nConstructor: bar_1\r\n=== lvalue ===\r\nCopy Constructor: bar_1\r\nMove Assignment: bar_1\r\n=== rvalue ===\r\nMove Constructor: bar_1\r\nMove Assignment: bar_1\r\n\r\nrun_const_ref_store\r\nConstructor: bar_2\r\n=== lvalue ===\r\nCopy Assignment: bar_2\r\n=== rvalue ===\r\nCopy Assignment: bar_2\r\n\r\nrun_ref_store\r\nConstructor: bar_3\r\n=== lvalue ===\r\nCopy Assignment: bar_3\r\n=== rvalue ===\r\nnot possible\r\n\r\nrun_uni_ref_store\r\nConstructor: bar_4\r\n=== lvalue ===\r\nCopy Assignment: bar_4\r\n=== rvalue ===\r\nMove Assignment: bar_4\r\n\r\nrun_const_uni_ref_store\r\nConstructor: bar_5\r\n=== lvalue ===\r\nnot possible\r\n=== rvalue ===\r\nCopy Assignment: bar_5\r\n<\/pre>\n<\/details>\n<p>Not store copy<\/p>\n<table>\n<colgroup>\n<col span=\"1\" style=\"width: 10%;\">\n<col span=\"1\" style=\"width: 18%;\">\n<col span=\"1\" style=\"width: 18%;\">\n<col span=\"1\" style=\"width: 18%;\">\n<col span=\"1\" style=\"width: 18%;\">\n<col span=\"1\" style=\"width: 18%;\">\n  <\/colgroup>\n<tr>\n<th><\/th>\n<th>copy_<wbr>no_store<\/th>\n<th>const_ref_<wbr>no_store<\/th>\n<th>ref_<wbr>no_store<\/th>\n<th>uni_ref_<wbr>no_store<\/th>\n<th>const_uni_ref_<wbr>no_store<\/th>\n<\/tr>\n<tr>\n<th>lvalue<\/th>\n<td bgcolor=\"lightyellow\">1 Copy<\/td>\n<td bgcolor=\"lightgreen\">&#8211;<\/td>\n<td bgcolor=\"lightgreen\">&#8211;<\/td>\n<td bgcolor=\"lightgreen\">&#8211;<\/td>\n<td bgcolor=\"lightcoral\">Not possible<\/td>\n<\/tr>\n<tr>\n<th>rvalue<\/th>\n<td bgcolor=\"lightgreen\">1 Move<\/td>\n<td bgcolor=\"lightgreen\">&#8211;<\/td>\n<td bgcolor=\"lightcoral\">Not possible<\/td>\n<td bgcolor=\"lightgreen\">&#8211;<\/td>\n<td bgcolor=\"lightgreen\">&#8211;<\/td>\n<\/tr>\n<\/table>\n<p>Store copy<\/p>\n<table>\n<colgroup>\n<col span=\"1\" style=\"width: 10%;\">\n<col span=\"1\" style=\"width: 18%;\">\n<col span=\"1\" style=\"width: 18%;\">\n<col span=\"1\" style=\"width: 18%;\">\n<col span=\"1\" style=\"width: 18%;\">\n<col span=\"1\" style=\"width: 18%;\">\n  <\/colgroup>\n<tr>\n<th><\/th>\n<th>copy_<wbr>store<\/th>\n<th>const_ref_<wbr>store<\/th>\n<th>ref_<wbr>store<\/th>\n<th>uni_ref_<wbr>store<\/th>\n<th>const_uni_ref_<wbr>store<\/th>\n<\/tr>\n<tr>\n<th>lvalue<\/th>\n<td bgcolor=\"lightgreen\">1 Copy<br \/>1 Move<\/td>\n<td bgcolor=\"lightgreen\">1 Copy<\/td>\n<td bgcolor=\"lightgreen\">1 Copy<\/td>\n<td bgcolor=\"lightgreen\">1 Copy<\/td>\n<td bgcolor=\"lightcoral\">Not possible<\/td>\n<\/tr>\n<tr>\n<th>rvalue<\/th>\n<td bgcolor=\"lightgreen\">2 Moves<\/td>\n<td bgcolor=\"lightyellow\">1 Copy<\/td>\n<td bgcolor=\"lightcoral\">Not possible<\/td>\n<td bgcolor=\"lightgreen\">1 Move<\/td>\n<td bgcolor=\"lightyellow\">1 Copy<\/td>\n<\/tr>\n<\/table>\n<\/details>\n<hr>\n<p><strong>Result<\/strong><\/p>\n<p>Decision table<\/p>\n<table>\n<tr>\n<th>Small object?<\/th>\n<th>Modify original?<\/th>\n<th>Store copy?<\/th>\n<th>Result<\/th>\n<\/tr>\n<tr>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<th>uni_ref_store<\/th>\n<\/tr>\n<tr>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<td>No<\/td>\n<th>uni_ref_no_store<\/th>\n<\/tr>\n<tr>\n<td>Yes<\/td>\n<td>No<\/td>\n<td>Yes<\/td>\n<th>copy_store<\/th>\n<\/tr>\n<tr>\n<td>Yes<\/td>\n<td>No<\/td>\n<td>No<\/td>\n<th>copy_no_store<\/th>\n<\/tr>\n<tr>\n<td>No<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<th>uni_ref_store<\/th>\n<\/tr>\n<tr>\n<td>No<\/td>\n<td>Yes<\/td>\n<td>No<\/td>\n<th>uni_ref_no_store<\/th>\n<\/tr>\n<tr>\n<td>No<\/td>\n<td>No<\/td>\n<td>Yes<\/td>\n<th>copy_store<\/th>\n<\/tr>\n<tr>\n<td>No<\/td>\n<td>No<\/td>\n<td>No<\/td>\n<th>const_ref_no_store<\/th>\n<\/tr>\n<\/table>\n<hr>\n<p>Further information: <a href=\"http:\/\/www.max-sperling.bplaced.net\/?p=9881\">When and how to pass smart pointer (C++)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Analysis Code \u25bc Output \u25bc Constructor: bar_0 Not store copy run_copy_no_store Constructor: bar_1 === lvalue === Copy Constructor: bar_1 ===<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18379"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18379"}],"version-history":[{"count":50,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18379\/revisions"}],"predecessor-version":[{"id":18441,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18379\/revisions\/18441"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18379"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}