
{"id":4475,"date":"2018-12-24T23:52:40","date_gmt":"2018-12-25T04:52:40","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4475"},"modified":"2019-03-04T23:54:53","modified_gmt":"2019-03-05T04:54:53","slug":"forth-to-the-past-passing-t-as-a-parameter","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4475","title":{"rendered":"Forth to the Past: passing T&#038;&#038; as a parameter"},"content":{"rendered":"\n<p>This is a continuation of the series about C++ rvalue references. <a href=\"https:\/\/ikriv.com\/blog\/?p=2559\">First post<\/a>, <a href=\"https:\/\/ikriv.com\/blog\/?p=2612\">second post<\/a>, <a href=\"https:\/\/ikriv.com\/blog\/?p=2618\">third post<\/a>. Rvalue references are unique, because if you have a variable of type T&amp;&amp;, you cannot pass it to a function that accepts a T&amp;&amp;.&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class SomeClass;<br>void f(SomeClass&amp;&amp; ref);<br>void g(SomeClass&amp;&amp; ref) <br>{ <br>    f(ref); <font color=\"green\">\/\/ error C2664: cannot convert argument 1 from 'SomeClass' to 'SomeClass &amp;&amp;'<\/font><br>}<br><\/pre>\n\n\n\n<p>Of course, there is a good explanation for this, but it still feels like a violation of fundamental violation of software development. If you have a function that accepts bananas, and you have a banana, you should be able to pass your banana to that function, shouldn&#8217;t you? Of course, even prior to rvalue references you could stop this from happening, e.g. by&nbsp; making copy constructor private. Consider:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class SomeClass { SomeClass(SomeClass const&amp;); \/* private copy constructor *\/ };<br>void f(SomeClass c);<br>void g(SomeClass c) <br>{ <br>    f(c); <font color=\"green\">\/\/ error C2248: cannot access private member declared in class 'SomeClass'<\/font><br>}<\/pre>\n\n\n\n<p>But that&#8217;s different, since in that case things work as expected by default, and you need to make an effort to break them. With rvalues things are weird by default.<\/p>\n\n\n\n<p>The reason for that is that rvalue is by definition something without a name. Once we create a parameter out of it and give it a name (&#8220;ref&#8221; in our example), it stops being an rvalue. &#8220;ref&#8221; binds to an rvalue, but is in itself an lvalue. We can say <code>ref=42;<\/code> and it would be legal. Rvalue-ness is not transitive. To convert &#8220;ref&#8221; back to rvalue, we need to use either <code>move()<\/code> or <code>forward()<\/code>. E.g. this works:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class SomeClass;<br>void f(SomeClass&amp;&amp; ref);<br>void g(SomeClass&amp;&amp; ref) <br>{ <br>    f(<strong>std::move<\/strong>(ref)); <br>}<\/pre>\n\n\n\n<p>See also: <a href=\"https:\/\/stackoverflow.com\/questions\/9671749\/whats-the-difference-between-stdmove-and-stdforward\">what is the difference between move() and forward()<\/a>?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a continuation of the series about C++ rvalue references. First post, second post, third post. Rvalue references are unique, because if you have a variable of type T&amp;&amp;, <a href=\"https:\/\/ikriv.com\/blog\/?p=4475\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[7],"tags":[],"class_list":["entry","author-ikriv","post-4475","post","type-post","status-publish","format-standard","category-cpp"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4475"}],"version-history":[{"count":4,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4475\/revisions"}],"predecessor-version":[{"id":4497,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4475\/revisions\/4497"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}