Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>is there some other clause in the language specification that this exploits?</p> </blockquote> <p>Yes, see 12.8 §34 and §35:</p> <blockquote> <p>When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object [...] This elision of copy/move operations, called <em>copy elision</em>, is permitted [...] in a return statement in a function with a class return type, <strong>when the expression is the name of a non-volatile automatic object</strong> with the same cv-unqualified type as the function return type [...]</p> <p>When the criteria for elision of a copy operation are met and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed <strong>as if the object were designated by an rvalue</strong>.</p> </blockquote> <hr> <p>Just wanted to add one more point that returning by value should be the default choice here because a named value in the return statement in the worst case, i.e. without elisions in C++11, C++14 and C++17 is treated as an rvalue. So for example the following function compiles with the <code>-fno-elide-constructors</code> flag</p> <pre><code>std::unique_ptr&lt;int&gt; get_unique() { auto ptr = std::unique_ptr&lt;int&gt;{new int{2}}; // &lt;- 1 return ptr; // &lt;- 2, moved into the to be returned unique_ptr } ... auto int_uptr = get_unique(); // &lt;- 3 </code></pre> <p>With the flag set on compilation there are two moves (1 and 2) happening in this function and then one move later on (3). </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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