Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No.</p> <p>The C++ language specification never makes such a straightforward assertion as the one you are asking about. It doesn't say anywhere in the language standard that "all temporary objects are rvalues". Moreover, the question itself is a bit of misnomer, since the property of being an rvalue in the C++ language is not a property of an object, but rather a property of an expression (i.e. a property of its result). This is actually how it is defined in the language specification: for different kinds of expressions it says when the result is an lvalue and when it is an rvalue. Among other things, this actually means that a temporary object can be accessed as an rvalue as well as an lvalue, depending on the specific form of expression that is used to perform the access.</p> <p>For example, the result of literal <code>2 + 3</code> expression is obviously an rvalue, a temporary of type <code>int</code>. We cannot apply the unary <code>&amp;</code> to it since unary <code>&amp;</code> requires an lvalue as its operand</p> <pre><code>&amp;(2 + 3); // ERROR, lvalue required </code></pre> <p>However, as we all know, a constant reference can be attached to a temporary object, as in</p> <pre><code>const int &amp;ri = 2 + 3; </code></pre> <p>In this case the reference is attached to the temporary, extending the lifetime of the latter. Obviously, once it is done, we have access to that very same temporary as an lvalue <code>ri</code>, since references are always lvalues. For example, we can easily and legally apply the unary <code>&amp;</code> to the reference and obtain a pointer to the temporary </p> <pre><code>const int *pi = &amp;ri; </code></pre> <p>with that pointer remaining perfectly valid as long as the temporary persists.</p> <p>Another obvious example of lvalue access to a temporary object is when we access a temporary object of class type through its <code>this</code> pointer. The result of <code>*this</code> is an lvalue (as is always the case with the result of unary <code>*</code> applied to a data pointer), yet it doesn't change the fact that the actual object might easily be a temporary. For a given class type <code>T</code>, expression <code>T()</code> is an rvalue, as explicitly stated in the language standard, yet the temporary object accessed through <code>*T().get_this()</code> expression (with the obvious implementation of <code>T::get_this()</code>) is an lvalue. Unlike the previous example, this method allows you to immediately obtain a non-const-qualified lvalue, which refers to a temporary object.</p> <p>So, once again, the very same temporary object might easily be "seen" as an rvalue or as an lvalue depending on what kind of expression (what kind of <em>access path</em>) you use to "look" at that object.</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.
 

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