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

"No viable overloaded =" nullptr

I have just started with C++ and am stuck on the move constructor. Here is my .cpp: SimpleMatrix::SimpleMatrix(SimpleMatrix &&other_mat) { cols = other_mat.cols; rows = other_mat.rows; data_ = other_mat.data_; other_mat.cols = 0; other_mat.rows…
-1
votes
1 answer

Type deduction for rvalues in templates

Requesting some help to understand the type deduction of rvalue reference. The non-templated version fails with the following error and I understand the reason. error: cannot bind non-const lvalue reference of type 'const char*&' to an rvalue of…
-1
votes
1 answer

Overload a class constructor that takes an rvalue reference

I would like to do something like the following: class Foo { Foo(int &&a, int b, std::string s=""); // does not compile because a is not an rvalue: // Foo(int &&a, std::string s) : Foo(a, 0, s) {} Foo(int &&a, std::string s) :…
-1
votes
1 answer

how can I bind a class member function with param as rvalue to boost::function?

I try to bind a class member function with param as rval to boost::function. But it doesn't work. my sample false code : class Class1 { int Foo1(int&& b) { return b; } void foo2() { boost::function
庄嘉琪
  • 21
  • 2
-1
votes
1 answer

How to correctly transfer the ownership of a shared_ptr?

I have the following code snipet: // code snipet one: #include #include #include struct A { uint32_t val0 = 0xff; ~A() { std::cout << "item gets freed" << std::endl; } }; typedef…
-1
votes
2 answers

Yet another optimization with rvalue reference &&. Simply rename fields

Let's consider the next code: struct Channel; // somewhere declared using iter_t = std::set::iterator; std::set myset; std::pair emplaced = myset.emplace(arg1,arg2); Then emplaced.first contains iterator to…
kyb
  • 7,233
  • 5
  • 52
  • 105
-1
votes
2 answers

const auto && really not useful?

Here https://youtu.be/dTeKf5Oek2c?t=2939 Stephen T. Lavavej says in a talk that const auto && is not useful. Isn't the following a valid use case? NonMovableNonCopyable create () { // ... return {}; } const auto && obj = create (); Concrete…
JohnB
  • 13,315
  • 4
  • 38
  • 65
-1
votes
1 answer

rvalue reference to temporary declaration

E && e0 = E () ; E e1 ; is there any differences between these two cases of object declaration.? ;
Volodymyr Boiko
  • 1,533
  • 15
  • 29
-1
votes
1 answer

pointer-to-member-function type requires an rvalue

What is the matter of the error, which produces the following code? struct foo { void call(void (foo::*ptr)()) && { (*this.*ptr)(); } }; How to fix this error?
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
-1
votes
2 answers

Initialization of member array with noncopyable non pod

I think the most simple way to ask is due to an example. Assume we have the following type: class Node { // make noncopyable Node(const Node& ref) = delete; Node& operator=(const Node& ref) = delete; // but moveable Node(Node&& ref)…
-2
votes
1 answer

C++ template function and universal reference error?

What is the exact difference of universal reference and rvalue reference? The code below should except both rvalue reference and lvalue reference as argument. And apparently the first function for_each takes both successfully. However, when it calls…
-2
votes
1 answer

ELI5 What is the meaning '&&' keyword after a function name

I met this code and i dont know what is this, can someone explain to me? template base{ protected: T data; public: ... T&& unwrap() && { return std::move(data); } operator T&&() && { return std::move(data); } } I know operator T&&()…
Justin
  • 149
  • 1
  • 11
-2
votes
1 answer

std::vector::push_back(T&&) creates default constructed value of T when a function returning T&& is passed directly

I have a function that returns an rvalue reference. auto function() -> int&& { int x{10}; std::cout << x << std::endl; // to check the value of x return std::move(x); } Now, when I use the following code: std::cout << function() <<…
abhijit
  • 345
  • 1
  • 3
  • 11
-2
votes
1 answer

rvalue References and Template parameters

Why is this syntax not allowed: template struct MyStruct{ float GetValue() { return value; } }; MyStruct<12.1f> myFloat; And we have to instead do this: template struct MyStruct{ float GetValue() { return…
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
-2
votes
2 answers

why rvalue reference can be bind to a non reference type

int main() { int rx = 0; int ry = std::move(rx); //here is the point of my question int lx = 0; int ly = &lx; //(2) obviously doesn't compile std::cin.ignore(); } I'm a little bit lost with this aspect of rvalue, I can't…
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
1 2 3
72
73