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

Overload resolution between object, rvalue reference, const reference

Given all three functions, this call is ambiguous. int f( int ); int f( int && ); int f( int const & ); int q = f( 3 ); Removing f( int ) causes both Clang and GCC to prefer the rvalue reference over the lvalue reference. But instead removing…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
67
votes
1 answer

Workarounds for no 'rvalue references to *this' feature

I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy itself is being moved. I believe that I will be able to implement this…
boycy
  • 1,473
  • 12
  • 25
65
votes
2 answers

Can I typically/always use std::forward instead of std::move?

I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 minutes in that I was also wondering about. Meyers…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
52
votes
2 answers

Why user-defined move-constructor disables the implicit copy-constructor?

While I'm reading boost/shared_ptr.hpp, i saw this code: // generated copy constructor, destructor are fine... #if defined( BOOST_HAS_RVALUE_REFS ) // ... except in C++0x, move disables the implicit copy shared_ptr( shared_ptr const & r ): px(…
amazingjxq
  • 4,487
  • 7
  • 33
  • 35
51
votes
1 answer

Overload on reference, versus sole pass-by-value + std::move?

It seems the main advice concerning C++0x's rvalues is to add move constructors and move operators to your classes, until compilers default-implement them. But waiting is a losing strategy if you use VC10, because automatic generation probably won't…
dean
  • 51
  • 1
  • 4
  • 8
49
votes
6 answers

How to make template rvalue reference parameter ONLY bind to rvalue reference?

I'm writing a network library and use move semantics heavily to handle ownership for file descriptors. One of my class wishes to receive file descriptor wrappers of other kinds and take ownership, so it's something like struct OwnershipReceiver { …
Ralph Zhang
  • 5,015
  • 5
  • 30
  • 40
49
votes
4 answers

Should a move constructor take a const or non-const rvalue reference?

In several places I've seen the recommended signatures of copy and move constructors given as: struct T { T(); T(const T& other); T(T&& other); }; Where the copy constructor takes a const reference, and the move constructor takes a…
Ben Hymers
  • 25,586
  • 16
  • 59
  • 84
48
votes
2 answers

Why doesn't `std::stringstream::stringstream(std::string&&)` exist?

I was hoping stringstream has a constructor that steals its initial content from a string&&. Do such inter-species "move constructors" generally not exist in the STL? If not, why not?
Museful
  • 6,711
  • 5
  • 42
  • 68
45
votes
4 answers

Use of rvalue reference members?

I was wondering what use an rvalue reference member has class A { // ... // Is this one useful? Foo &&f; }; Does it have any benefits or drawbacks compared to an lvalue reference member? What is a prime usecase of it?
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
44
votes
4 answers

Avoid exponential grow of const references and rvalue references in constructor

I am coding some templated classes for a machine learning library, and I'm facing this issue a lot of times. I'm using mostly the policy pattern, where classes receive as template argument policies for different functionalities, for…
43
votes
6 answers

How to actually implement the rule of five?

UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly simplifies and beautifies it's usage? Or are not all five…
stijn
  • 34,664
  • 13
  • 111
  • 163
43
votes
4 answers

What is use of the ref-qualifier `const &&`?

I've been digging around ref-qualifiers a bit, following on a previous question. Given the code sample below; #include #include #include struct A { std::string abc = "abc"; std::string& get() & { std::cout <<…
Niall
  • 30,036
  • 10
  • 99
  • 142
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
41
votes
3 answers

Passing by value vs const & and && overloads

So after looking up move semantics I see that general consensus is to pass by value when you intend to transfer ownership. But in Scott Meyer's talk on Universal references I've noticed that std::vector::push_back has 2 overloads: void push_back(…
user3624760
39
votes
1 answer

Differences between std::is_convertible and std::convertible_to (in practice)?

According to en.cppreference.com (from what I can gather): std::is_convertible is a trait class requiring types From & To to be such that a function with return type To that returns a From value can compile. std::convertible_to is a concept…
Monad
  • 680
  • 8
  • 15
1
2
3
72 73