Questions tagged [rvalue]

An rvalue is a temporary object (or subobject) or is a value not directly associated with an object.

Traditionally known as r-values since they generally appear on the right hand side of an expression; rvalues are temporary object whose lifetime does not extend past the current expression (unless bound to a an appropriate reference see ).

An rvalue is not directly associated with an object. Often a simple test is applied to determine if is an rvalue; "Is it named?"; if so, it is not an rvalue.

C++11 extended the original definition of an rvalue to include prvalues (pure rvalues) and xvalues (expiring values).

For a more general post on value categories, see this post on SO.

718 questions
62
votes
3 answers

What makes moving objects faster than copying?

I have heard Scott Meyers say "std::move() doesn't move anything" ... but I haven't understood what it means. So to specify my question consider the following: class Box { /* things... */ }; Box box1 = some_value; Box box2 = box1; // value of…
Laith
  • 1,248
  • 2
  • 11
  • 19
59
votes
9 answers

Address of a temporary in Go?

What's the cleanest way to handle a case such as this: func a() string { /* doesn't matter */ } b *string = &a() This generates the error: cannot take the address of a() My understanding is that Go automatically promotes a local variable to…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
58
votes
1 answer

std::is_same different results between compilers

#include int main() { bool b = true; std::cout << std::is_same::value << "\n"; auto bb = (!(!b)); std::cout << std::is_same::value << "\n"; } The above code has different…
Nir
  • 1,608
  • 1
  • 17
  • 29
54
votes
4 answers

How to determine programmatically if an expression is rvalue or lvalue in C++?

What's the best way to determine if an expression is a rvalue or lvalue in C++? Probably, this is not useful in practice but since I am learning rvalues and lvalues I thought it would be nice to have a function is_lvalue which returns true if the…
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
51
votes
7 answers

Function that accepts both lvalue and rvalue arguments

Is there a way to write a function in C++ that accepts both lvalue and rvalue arguments, without making it a template? For example, suppose I write a function print_stream that reads from an istream and prints the data that was read to the screen,…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
49
votes
11 answers

Why is ++i considered an l-value, but i++ is not?

Why is ++i is l-value and i++ not?
yesraaj
  • 46,370
  • 69
  • 194
  • 251
45
votes
5 answers

Isn't the const modifier here unnecessary?

The "Effective C++" Item 3 says "Use const whenever possible", and it gives an example like: const Rational operator*(const Rational& lhs, const Rational& rhs); to prevent clients from being able to commit atrocities…
Joey.Z
  • 4,492
  • 4
  • 38
  • 63
43
votes
3 answers

Why and when does the ternary operator return an lvalue?

For a long time I thought that the ternary operator always returns an rvalue. But to my surprise it doesn't. In the following code I don't see the difference between the return value of foo and the return value of the ternary operator. #include…
Soulimane Mammar
  • 1,658
  • 12
  • 20
42
votes
4 answers

Rvalue Reference is Treated as an Lvalue?

I posted this answer: https://stackoverflow.com/a/28459180/2642059 Which contains the following code: void foo(string&& bar){ string* temp = &bar; cout << *temp << " @:" << temp << endl; } Is bar an rvalue or an lvalue? I ask because I…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
37
votes
5 answers

Why "universal references" have the same syntax as rvalue references?

I just made some research about those (quite) new features and I wonder why C++ Committee decided to introduce the same syntax for both of them? It seems that developers unnecessary have to waste some time to understand how it works, and one…
Piotrek
  • 451
  • 1
  • 5
  • 9
35
votes
4 answers

Do rvalue references allow dangling references?

Consider the below. #include using std::string; string middle_name () { return "Jaan"; } int main () { string&& danger = middle_name(); // ?! return 0; } This doesn't compute anything, but it compiles without error and…
Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
34
votes
7 answers

Taking the address of a temporary object

§5.3.1 Unary operators, Section 3 The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. What exactly does "shall be" mean in this context? Does it mean it's an error to take the address…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
34
votes
7 answers

Are all temporaries rvalues in C++?

I have been coding in C++ for past few years. But there is one question that I have not been able to figure out. I want to ask, are all temporaries in C++, rvalues? If no, can anyone provide me an example where temporary produced in the code is an…
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
33
votes
5 answers

Passing rvalues through std::bind

I want to pass an rvalue through std::bind to a function that takes an rvalue reference in C++0x. I can't figure out how to do it. For example: #include #include template void foo(Type &&value) { Type…
Timothy003
  • 2,348
  • 5
  • 28
  • 33
33
votes
2 answers

non-class rvalues always have cv-unqualified types

§3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&&…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
1
2 3
47 48