Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Passing the rvalue <code>rtByValue()</code> to a function that expects an lvalue reference doesn't work because this would require the lvalue reference argument to be initialized from an rvalue. §8.5.3/5 describes how lvalue references can be initialized &ndash; I won't quote it in full, but it basically says that an lvalue reference can be initialized</p> <ul> <li>either from another lvalue reference</li> <li>or something that can be converted to an lvalue reference of an intermediary type</li> <li>or from an rvalue, but only if the lvalue reference we initialize is a const-reference</li> </ul> <p>Since the argument we need to initialize is not a const-reference, none of this applies.</p> <p>On the other hand,</p> <pre><code>rtByValue() = aa; </code></pre> <p>i.e., assigning to a temporary object, is possible because of:</p> <blockquote> <p>(§3.10/5) An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to modify its referent under certain circumstances. [ Example: a member function called for an object (9.3) can modify the object. — end example ]</p> </blockquote> <p>So this works only because <code>A</code> is of class-type, and the (implicitly defined) assignment operator is a member function. (See <a href="https://stackoverflow.com/questions/6466253/if-temporaries-are-implicitly-non-modifiable-how-does-this-work">this related question</a> for further details.)</p> <p>(So, if <code>rtByValue()</code> were to return, for example, an <code>int</code>, then the assignment wouldn't work.)</p>
    singulars
    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.
    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