3

I think I'm missing something basic regarding the lvalue-to-rvalue standard conversion.

From C++11 4.1:

A glvalue of a non-function, non-array type T can be converted to a prvalue

So we declare a variable x:

 int x = 42;

An expression x in this scope is now an lvalue (so also a glvalue). It satisfies the requirements in 4.1 for an lvalue-to-rvalue conversion.

What is a typical example of a context where the lvalue-to-rvalue conversion is applied to the expression x?

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319

1 Answers1

5

A prvalue ("pure" rvalue) is an expression that identifies a temporary object (or a subobject thereof) or is a value not associated with any object.

struct Bar
{
    int foo()
    {
        int x = 42;
        return x;    // x is converted to prvalue
    }
};

the expression bar.foo() is a prvalue.

OR

Lambda expressions, such as

[](int x){return x*x;}

§ 3.10.1

A prvalue (“pure” rvalue) is an rvalue that is not an xvalue. [ Example: The result of calling a function whose return type is not a reference is a prvalue. The value of a literal such as 12, 7.3e5, or true is also a prvalue. —end example ]

see n3055.

billz
  • 44,644
  • 9
  • 83
  • 100
  • @JohannesSchaub-litb I am saying that "The result of calling a function whose return type is not a reference is a prvalue". – billz Jul 14 '13 at 11:40
  • @billz equivalent (please replace rvalue in my above comment by prvalue. accident). you are not calling a function in your code. – Johannes Schaub - litb Jul 14 '13 at 12:24
  • @JohannesSchaub-litb Updated my answer, `bar.foo()` is that what you meant calling a function? I won't say `foo` is prvalue. :) – billz Jul 14 '13 at 12:40
  • I will remove my -1, but want to note that despite a confusing Standard paragraph (which says that rvalues are "temporary objects"), *"A prvalue ("pure" rvalue) is an expression that identifies a temporary object"* is not true. Despite being able to refer to a temporary object by an lvalue indirectly, `const int& a = 10; &a;`, there are non-prvalues that directly refer to them aswell, see 15.1p3 *"Throwing an exception copy-initializes (8.5, 12.8) a temporary object, called the exception object. The temporary is an lvalue and is used to initialize the variable named in the matching handler"*. – Johannes Schaub - litb Jul 14 '13 at 13:52
  • However, that is a very subtle issue and I think your answer is fine as it is. Thanks for fixing it. – Johannes Schaub - litb Jul 14 '13 at 13:55