Note that there are some explanatory texts on larger screens.

plurals
  1. POShould all C++ functions be declared taking rvalue from now?
    text
    copied!<p>Why should I or shouldn't I create all my functions and members functions to take a <code>rvalue</code> and leave out versions that take a <code>lvalue</code>? You can always forward <code>lvalue</code> to rvalue, right? I can even have <code>const rvalue</code>, so why is this a bad idea, or a good idea?</p> <p>What I mean in code is below. The <code>"&amp;&amp;" rvalue</code> reference allows the users to use temporaries and can still use <code>lvalue</code> by a simple forward. So with this in mind, why should I provide <code>print_string(string&amp; str)</code> (<code>lvalue</code> reference) of any function in c++11 (besides <code>const</code> reference, because <code>rvalue</code> there is fine)?</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;utility&gt; using namespace std; void print_string(string&amp;&amp; str) { str += "!!!"; cout &lt;&lt; str &lt;&lt; endl; } void print_string2(string&amp; str) { str += "???"; cout &lt;&lt; str &lt;&lt; endl; } int main(int argc, char* argv[]) { print_string("hi there"); // works print_string(string("hi again")); // works string lvalue("lvalue"); print_string(forward&lt;string&gt;(lvalue)); // works as expected //print_string(lvalue); // compile error //print_string2("hi there"); // comile error //print_string2(string("hi again")); // compile error print_string2(lvalue); char a; cin &gt;&gt; a; } </code></pre> <h2>output</h2> <p>hi there!!! <br> hi again!!! <br> lvalue!!! <br> lvalue!!!???</p> <p><code>void print_string(string&amp;&amp; str)</code> provides a more flexible use case than <code>void print_string2(string&amp; str)</code> so why shouldn't we use rvalue arguments all the time?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload