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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@JohannesSchaub-litb I am saying that "The result of calling a function whose return type is not a reference is a prvalue".
@billz equivalent (please replace rvalue in my above comment by prvalue. accident). you are not calling a function in your code.
@JohannesSchaub-litb Updated my answer, bar.foo() is that what you meant calling a function? I won't say foo is prvalue. :)
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".
However, that is a very subtle issue and I think your answer is fine as it is. Thanks for fixing it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.