Questions tagged [rvalue-reference]

An rvalue reference is a new language feature in C++11 representing a reference to an rvalue. Together with reference collapsing, they are used to implement and enable move semantics and perfect forwarding.

An rvalue reference is a language feature added in C++11 (formerly known as C++0x). It is used to bind to an rvalue thus extending the temporary object's lifetime.

Together with reference collapsing, they are used to implement and enable move semantics and perfect forwarding.

1087 questions
11
votes
1 answer

Where are rvalues stored in C++?

I'm learning new C++ 11 features recently. However, I don't fully understand one thing about rvalues. Consider following code: string getText () { return "Fabricati diem"; } string newText = getText(); Call to getText() creates an r-value…
Kao
  • 7,225
  • 9
  • 41
  • 65
11
votes
5 answers

Pass by reference, constant reference, rvalue-reference, or constant rvalue-reference?

I was learning passing by reference, and here is the test I did: #include using namespace std; int i = 0; //If this is uncommented, compiler gives ambiguous definition error. //void paramCheck (string s) { // cout << ++i << ". Param…
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
11
votes
1 answer

Perfect forwarding for void and non-void returning functions

Previously I was using a macro to measure the time a function call took whenever I wanted to quickly check that. Now, with C++11 available, I would like to finally remove that ugly peace of preprocessor code and replace it with something like…
user2573221
  • 494
  • 4
  • 13
11
votes
3 answers

C++ constructor with rvalue reference

Consider this class with three constructors: class Circle { public: Circle(int r) { _radius = r; } Circle(const Circle& c){ _radius = c.radius(); cout << endl << "Copy constructor with lvalue reference. Radius: " <<…
PoP
  • 2,055
  • 3
  • 16
  • 20
10
votes
4 answers

C++: What are R-Value references on a technical level (ASM)?

Possible Duplicate: What is the difference between r-value references and l-value references? (CodeGen) I was wondering, can anyone explain what R-Value references are on a technical level? By that I mean: What happens on assembler level when…
PuerNoctis
  • 1,364
  • 1
  • 15
  • 34
10
votes
3 answers

Are moved-from objects required to be destructed?

If I move-construct a from b, is it still necessary to destruct b, or can I get away without doing so? This question crossed my mind during the implementation of an optional template. Excerpt: ~optional() { if (initialized) { …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
10
votes
1 answer

Rvalue reference or lvalue?

I've got a question of similiar nature like this one posted 5 years ago: Why are rvalues references variables not rvalue? My major concern is why can I do this: int&& k = 3; k++; but I cannot do this: (static_cast(3))++; I've always…
domdrag
  • 541
  • 1
  • 4
  • 13
10
votes
1 answer

C++11 vector makes copies on rvalue

I have some strange behavior on std::vector with adding rvalues: #include #include class A { public: A():i_{5}{std::cout << "default" << std::endl;} A(const A &data) { std::cout << "copied!" << std::endl; } A(A…
Sorcerer
  • 190
  • 7
10
votes
1 answer

Can someone explain rvalue references with respect to exceptions?

Lets say I've this exception class: struct MyException : public std::exception { MyException(const std::exception &exc) : std::exception(exc) { cout << "lval\n"; } MyException(std::exception &&exc) :…
legends2k
  • 31,634
  • 25
  • 118
  • 222
10
votes
1 answer

C++: conversions of lvalue references and rvalue references

I wonder which parts of the standard specify that in the following code segment: #include class A { }; class B : public A { }; int main() { std::unique_ptr bptr = std::make_unique(); // (a) std::unique_ptr aptr =…
apriori
  • 1,260
  • 11
  • 29
10
votes
4 answers

Passing rvalue as reference

I have some Qt code that I downloaded from my svn repo. It's a while since I worked on it but I am sure it used to compile. I have a new version of Qt and compiler (to what I had in the last time). My current compiler is: mingw 4.9.2 32-bit. So here…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
10
votes
2 answers

Qt 5: unable to declare signal that takes rvalue reference

Probably I've missed something, but I can't find any information that signals can't take rvalue references. So, I have a class with the following signal declaration: signals: void messageDecoded(HTDataMsg &&msg); When I try to compile it, I got…
Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
10
votes
1 answer

Overload between rvalue reference and const lvalue reference in template

I want to overload two functions based on whether the argument is a temporary object, so I write code like this: #include void f(int &&) { std::cout << "&&" << std::endl; } void f(const int&) { std::cout << "const &" <<…
delphifirst
  • 1,781
  • 1
  • 14
  • 23
10
votes
3 answers

Problem with "moveable-only types" in VC++ 2010

I recently installed Visual Studio 2010 Professional RC to try it out and test the few C++0x features that are implemented in VC++ 2010. I instantiated a std::vector of std::unique_ptr, without any problems. However, when I try to populate it by…
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
10
votes
2 answers

Using rvalue references for default arguments

I want to make a function that takes an optional reference to an object, and creates one for the duration of the function if it is not provided, i.e. void Foo(Bar& b = Bar()) { /* stuff */ } This is, of course, invalid code, as a Bar cannot be…