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
2 answers

Boost.Move integration with other Boost libraries

Rvalue references generally boost performance in a C++ program. But they are not available directly in a C++03 compiler. Fortunately boost::move seems to be able to emulate it even in C++03: Rvalue references are a major C++0x feature, enabling…
RoundPi
  • 5,819
  • 7
  • 49
  • 75
0
votes
1 answer

When are rvalue references to primitive integers short-lived or long-lived?

I have done some experimentation on rvalue references with the TDM-GCC 4.6.1 compiler and made some interesting observations that I cannot explain away with theories. I would like experts out there to help me explain them. I have a very simple…
softwarelover
  • 1,009
  • 1
  • 10
  • 22
-1
votes
1 answer

Pass parameter pack to function with template deduction from return type

I'm writing an object allocator that I'd like to call in the following way: T result = factoryObject.construct(argA, argB, argC); I currently have this design, which works... class Factory { void* memPool_; template
-1
votes
0 answers

How to move string into unordered_map?

I want to understand how to use emplace() method in unordered_map I have two functions foo1 and foo2, did I understand correctly that I don't have extra copy only in foo2? How can I check it? My code struct A { void foo1(const std::string& s) { …
mascai
  • 1,373
  • 1
  • 9
  • 30
-1
votes
1 answer

In the C++ 20 specification, where is the rule saying an rvalue reference cannot bind to an lvalue?

Of course, this will not compile: struct X {}; X&& x = X(); X&& x2 = x; // error: rvalue reference to type 'X' cannot bind to lvalue of type 'X' Where is the relevant rule to this in the C++20 specification? I read the section '9.4.3 References',…
relent95
  • 3,703
  • 1
  • 14
  • 17
-1
votes
1 answer

How to perfectly forward a universal reference that is either a const ref or a movable rvalue?

I have coded a lock-free and thread-safe ring queue with C++20, and it works so far. The only thing is not perfect that it has to have two enque() methods, one accepts a const reference to a lvalue as a argument, and the other accpects a reference…
-1
votes
1 answer

rvalue Reference not matching

I have this exercise in which we try various combinations of rvalue and lvalue references using a template class, I am getting two assertion errors; if someone could guide. #include typedef int& IntLRef; typedef IntLRef&…
AKM
  • 34
  • 6
-1
votes
1 answer

Regarding R values

#include #include #include #include "Person.h" #include "SmartPerson.h" using namespace std; void print_name(const Person& test); void print_name_2(const Person&& test); int main() { print_name(Person{ 21, "Juan",…
Brad
  • 17
  • 6
-1
votes
1 answer

C++ Avoid Temporary Object Destruction using Copy Constructor

I have a std::vector of Trees, a custom type, and I have a loop which pushes back a tot of Trees to the vector. The problem is: I need push back temporary Tree objects, but, at the same time, I have to avoid them getting deallocated (I thought…
-1
votes
1 answer

Defining uninitilized reference c++

I have a template T templateundefined_behavior(void) { throw; return T{}; } template struct UBHandler { const bool valid; T value; (operator T)() {if(valid)return value;return…
haelmic
  • 541
  • 4
  • 18
-1
votes
1 answer

Using move semantics: rvalue reference as a method parameter

I would like to doublecheck my understanding of move semantics. Am I missing anything in my reasoning: #include using std::cout; struct A { A() {cout<<"Constructor\n";} ~A() {cout<<"Desctuctor\n";} A(const A&) {cout<<"Copy…
-1
votes
2 answers

Why does ranges-v3 not move elements even the iterator is std::move_iterator?

#include #include #include #include using namespace std::literals; int main() { auto src = std::vector{"123"s, "456"s, "789"s}; auto movable_rng = ranges::subrange( …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
-1
votes
2 answers

Using RAII to simplify passing objects to other objects without copy

extern class objx; class Someclass { public: Someclass(); void bar(objx); objx &om; }; void foo() { Someclass c; objx o; c.bar(o); } void Someclass::bar(objx& op) { //Save op in some local privatefield for later use om…
Siavoshkc
  • 346
  • 2
  • 16
-1
votes
3 answers

get know whether 'this' or other arguments are rvalue

Suppose we have class Base and its member function Base doSomething(const Base& other). I would like to know how to determine whether this or other object is rvalue, for example I need something like Base Base::doSomething(const Base& other) { …
-1
votes
2 answers

Overloading for rvalue?

Below is a code demonstrating the question: class X {}; X x; X&& rvalue_ref = std::move(x); static_assert(std::is_same::value, "Different types"); //To be sure that type is X&& void func(X&) { cout << "lvalue…