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
13
votes
1 answer

Is a data member of a temporary object an xvalue in C++11?

#include using namespace std; struct A { vector coll; }; void f(const vector&){} void f(vector&&){} int main() { f(A().coll); // Is "A().coll" an xvalue? } Does C++11 guarantee f(A().coll) will call void…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
13
votes
2 answers

Take ownership of parameter by rvalue-reference

I want to make clear that the constructor of my class A will take ownership of the passed Data parameter. The obvious thing to do is take a unique_ptr by value: class A { public: A(std::unique_ptr data) : _data(std::move(data)) { } …
lt.ungustl
  • 211
  • 1
  • 8
13
votes
2 answers

Return forwarding reference parameters - Best practice

In the following scenario template ? f(T&& a, T&& b) { return a > b ? a : b; } what would the optimum return type be ? My thoughts so far are : Return by r value refs, perfectly forwarding the function arguments : template
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
13
votes
1 answer

Why does this function return an lvalue reference given rvalue arguments?

The following definition of a min function template constexpr auto min(T&& t, U&& u) -> decltype(t < u ? t : u) { return t < u ? t : u; } has a problem: it seems that it's perfectly legal to write min(10, 20) = 0; This…
13
votes
2 answers

Should implicitly generated assignment operators be & ref-qualified?

The following code compiles without problem on gcc 4.8.1: #include struct foo { }; int main() { foo bar; foo() = bar; foo() = std::move( bar ); } It seems the implicitly generated assignment operators for foo are not &…
Andrew Durward
  • 3,771
  • 1
  • 19
  • 30
13
votes
2 answers

When is the move constructor called in the `std::move()` function?

The function std::move() is defined as template typename std::remove_reference::type&& move(T && t) { return static_cast::type&&>( t ); } There are four places where I can imagine the move…
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
13
votes
2 answers

Would you ever mark a C++ RValue reference parameter as const

I've been switching Template Factory functions to use (and understand) std::forward to support rvalues and move semantics. My usual boilerplate factory functions for template classes have always marked the parameters as const: #include…
Joel
  • 2,928
  • 2
  • 24
  • 34
13
votes
3 answers

Is it meaningless to declare the return type of a function as T&&?

As we have known, in most common cases, T&& means "this is a temporary object". However, if one wants to return a temporary object from a function, he/she can declare the function as follows: template T f() { T t; ...... return…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
12
votes
1 answer

Which Boost Libraries take advantage of Move Semantics

Rvalue references and Move semantics are a major C++11 feature that can significantly speed up code by reducing unnecessary copies. The STL has been updated to use this new feature when a c++11/0x compiler is used (e.g. gcc 4.6) Boost 1.48…
mark
  • 7,381
  • 5
  • 36
  • 61
12
votes
2 answers

Why is an enum variable an rvalue here?

Example: typedef enum Color { RED, GREEN, BLUE } Color; void func(unsigned int& num) { num++; } int main() { Color clr = RED; func(clr); return 0; } I get the following error when I compile this: : In function…
Koen
  • 311
  • 2
  • 11
12
votes
2 answers

Reason to use std::move on rvalue reference parameter

I was reading a book about data structure implemented in C++, I dont understand a code snippet, it's part of vector class void push_back(object &&x) { //do something objects[size++] = std::move(x); } I know that std::move…
12
votes
1 answer

Why doesn't C++ move construct rvalue references by default?

Say I have the following function void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } Why do I need to cast param back to an rvalue with std::move()? Shouldn't it be obvious that the type of…
barney
  • 2,172
  • 1
  • 16
  • 25
12
votes
1 answer

Creating an object, local variable vs rvalue reference

Is there any advantage to using an r value reference when you create an object, that would otherwise be in a normal local variable? Foo&& cn = Foo(); cn.m_flag = 1; bar.m_foo = std::move(cn); //cn is not used again Foo cn; cn.m_flag = 1; bar.m_foo…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
12
votes
3 answers

Can I use rvalue reference to temporary? Is it undefined behavior or not?

Updating the question Why this two rvalue references examples have different behavior?: Source code: int a = 0; auto && b = a++; ++a; cout << a << b << endl; prints 20 Is it undefined behavior (UB) to use b after the a++ call? Maybe we cannot use b…
vladon
  • 8,158
  • 2
  • 47
  • 91
12
votes
1 answer

Debugging C++11 rvalue references with gdb

I just noticed that I can not debug rvalue references with gdb-7.7.1 properly. void simple(int &&i) {} When I enter this minimalistic function I can not obtain any meaningful information about i. It's type and value are unknown to…
oo_miguel
  • 2,374
  • 18
  • 30