Samik Samik - 1 year ago 79
C++ Question

Can't bind lvalue to rvalue reference

I have this C++ test code snippet,

#include <vector>

class A {
std::vector<int> x;
A(std::vector<int>&& _x) : x(_x) {}

class B {
A a;
B(std::vector<int>&& _x) : a(/*move(*/_x/*)*/) {}

I'm passing
to B as rvalue reference, but it's getting converted to lvalue when passed into
's constructor and I have to use
to make it work. My question is why _x is lvalue and not an rvalue reference in

Answer Source

Quote from WIKI

For safety reasons, some restrictions are imposed. A named variable will never be considered to be an rvalue even if it is declared as such. To get an rvalue, the function template std::move() should be used. Rvalue references can also be modified only under certain circumstances, being intended to be used primarily with move constructors.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download