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
0
votes
0 answers

Pimpl-like encapsulation-oriented usage pattern

I'd like to make only my implementation dependent on another header. hdr: // FDS.h #include "MyMath.h" union FancyDataStructure() { FancyDataStructure(): state(MyIdentityMat4), fanciful(0.31337) {} struct { MyMat4 state; float fanciful;…
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
0
votes
2 answers

Which type to declare to avoid copy and make sure returned value is moved

Suppose that I have a function A f(); I want to initialize a local variable a to the return value of f; I don't want to rely on RVO; What is the best option (and why) to avoid the return value of f being copied when a may have to be modified I…
ricab
  • 2,697
  • 4
  • 23
  • 28
0
votes
3 answers

Is the introduction of rvalue references actually useful?

The typical reasoning behind the introduction of rvalue references into C++ is to eliminate (optimize) superfluous copying during the evaluation of complex C++ expressions. However, there are two compiler optimization techniques for C++98/C++03,…
user2545918
0
votes
2 answers

How to localize places for introducing move semantics in a legacy code base?

We have a pretty huge code base, sometimes with performance issues. Move semantics is not used at all. I am wondering how could I find places where move semantics might be useful. Do you have any experience how to localize such a places?
pappati
  • 171
  • 1
  • 9
0
votes
0 answers

What is the use of const rvalue references?

I stumbled on the following code, and as I understand it, it shows a const rvalue reference. What is the use of such a construct, and what do it gain over a normal const lvalue reference? void foo(const bar&& b);
dalle
  • 18,057
  • 5
  • 57
  • 81
0
votes
2 answers

Getting the move constructor/assignment operator to be used

Consider the following code: #include #define P_(x) std::cout << x << std::endl class B { public: B() { P_("B::B()"); } B(const B&) { P_("B::B(const B&)"); } B(B&&) { P_("B::B(B&&)"); } ~B() { P_("B::~B()"); } B&…
Symaxion
  • 785
  • 7
  • 21
0
votes
1 answer

Generic macro code to pass regular and r-value ref arguments

A very simplified version of my code is this: #define proxy(name, type, arg) \ void name(function_list_object* obj, type arg) \ { \ void (*foo)(type arg) = obj->find_ptr_for(STRINGIZE(name)); \ foo(arg); \ } \ proxy(fi,…
Borislav Stanimirov
  • 1,609
  • 12
  • 23
0
votes
0 answers

Returning an object will it use move constructor/copy constructor?

X is a class X operator+() { X a; return a; } Book iam reading(ivor horton C++)says that when this function is called and the move constructor has been implemented for the class 'X' ,class constructor and move constructor are called. if the move…
0
votes
2 answers

Implementing a move constructor(rvalue reference) for an array class

I have an array class I grabbed off of a website that gives an example of a move constructor. How would one implement this move constructor in an example program however? I feel like I understand the function definition, but I have no idea how one…
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
0
votes
1 answer

Can't invoke or assign a std::function that has an rvalue reference as an argument (Visual C++)

Seems like Visual C++'s std::function<> doesn't handle functions with rvalue refs as arguments. Can anyone suggest a workaround? #include using namespace std; class Object { }; void f(Object&&) { } auto g = [](Object&&){…
Tim Culver
  • 145
  • 5
0
votes
3 answers

Forward or Move

Are these valid usage of move and forward? Are f3 and f4 the same? Is it dangerous to do so? Thank you! #include class A {}; A f1() { A a; return a; // Move constructor is called } A f2(A&& a) { return a; // Copy constructor…
yufanyufan
  • 171
  • 1
  • 7
0
votes
1 answer

Choose function declaration with lvalue or rvalue parameter

Is there a way to remove the 'plumb' version of all of my functions, without the need to change the 'hit' line to the 'fixed'? Yes my program works fine, but I think if is there a way to get ride from this version of all of my functions. Keep in…
Chameleon
  • 1,804
  • 2
  • 15
  • 21
0
votes
3 answers

Returning container from function: optimizing speed and modern style

Not entirely a question, although just something I have been pondering on how to write such code more elegantly by style and at the same time fully making use of the new c++ standard etc. Here is the example Returning Fibonacci sequence to a…
woosah
  • 843
  • 11
  • 22
0
votes
3 answers

Returning a mutable value in C++11

I was wondering about having a method return an r-value. Specifically, I was wondering if there was a way to do this with an overloaded operator. I have this code: struct vec4 { float x; float y; float z; float w; ... inline…
jepugs
  • 445
  • 4
  • 10
0
votes
2 answers

Binding rvalue-reference to a local variable in VC++

I'm using VC++2012 to run the following code: #include struct A { int* m_p; A() { m_p = new int; } ~A() { delete m_p; } A(const A& otherA) { m_p = new int; // BOOM! *m_p = *otherA.m_p; …
Ofek Shilon
  • 14,734
  • 5
  • 67
  • 101