更新时间:2023-11-12 13:50:04
每个命名对象都是Lvalue 有关左值的参考:
Every named object is Lvalue ref about Lvalue:
变量,函数,模板参数对象的名称(自C ++ 20)或数据成员(无论类型如何),例如std :: cin或std :: endl.即使变量的类型是右值引用,由名称组成的表达式是左值表达式;
vector
有两个赋值运算符重载,一个重载Lvalue引用,另一个重载Rvalue引用.
vector
has two overloads of assignment operator, one for Lvalue reference and another for Rvalue reference.
vector::operator=(const vector&) // copy assignment operator
vector::operator=(vector&&) // move assignment operator
当将Lvalue作为 operator =
的参数传递时,将调用采用Lvalue引用的
重载.此处的详细信息
Overload which takes Lvalue reference is called when Lvalue is passed as argument for operator=
.
Details here
当一个函数同时具有右值引用和左值引用时重载,右值引用重载绑定到右值(包括prvalue和xvalues),而左值引用重载绑定到左值
when a function has both rvalue reference and lvalue reference overloads, the rvalue reference overload binds to rvalues (including both prvalues and xvalues), while the lvalue reference overload binds to lvalues
通过 std :: move(rv);
,您将 rv
-左值转换为右值引用,而使用右值引用的 operator =
是叫.否则,左值将绑定到左值参考,并且矢量将被复制而不是被移动.
By std::move(rv);
you cast rv
- Lvalue to Rvalue reference, and operator=
which takes Rvalue reference is called. Otherwise, Lvalue binds to Lvalue reference and vector is copied instead of being moved.