为什么不动的对象仍然是副本
考虑以下代码,实体对象是不可移动的。我知道STD ::移动(OBJ)只是将OBJ投入到rvalue参考obj中。我还知道,rvalue参考变量仍然是一个lvalue对象。但是我仍然让为什么语句auto temp = std :: move(a)
可以调用复制构造函数,语句a = a = std :: move(b);
可以调用复制分配操作员。由于STD :: Move(a)是RVALUE参考,因此为什么它仍然可以调用LVALUE构造函数。
#include <iostream>
template<typename T>
void my_swap(T& a, T& b) {
auto temp = std::move(a);
a = std::move(b);
b = std::move(temp);
}
class Entity{
int _id;
public:
Entity(int id) :_id(id) {std::cout << "construtor\n" << std::endl;}
Entity(const Entity& other) {
_id = other._id;
std::cout << "copy constructor\n" << std::endl;
}
Entity& operator= (const Entity& other) {
if (&other == this) return *this;
this->_id = other._id;
std::cout << "copy assignment operator\n";
return *this;
}
};
int main() {
Entity e1 = Entity(10);
Entity e2 = Entity(20);
my_swap(e1,e2);
return 0;
}
Consider the following code, Entity object is non-movable. I know that std::move(Obj) just cast the Obj to a rvalue reference Obj. And I also know that rvalue reference variable is still a lvalue object. But I still confusing why statement auto temp = std::move(a)
can call copy constructor, statement a = std::move(b);
can call copy assignment operator. Since, std::move(a) is a rvalue reference, why it can still call lvalue constructor.
#include <iostream>
template<typename T>
void my_swap(T& a, T& b) {
auto temp = std::move(a);
a = std::move(b);
b = std::move(temp);
}
class Entity{
int _id;
public:
Entity(int id) :_id(id) {std::cout << "construtor\n" << std::endl;}
Entity(const Entity& other) {
_id = other._id;
std::cout << "copy constructor\n" << std::endl;
}
Entity& operator= (const Entity& other) {
if (&other == this) return *this;
this->_id = other._id;
std::cout << "copy assignment operator\n";
return *this;
}
};
int main() {
Entity e1 = Entity(10);
Entity e2 = Entity(20);
my_swap(e1,e2);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
。即使没有移动构造函数/分配操作员,它也具有复制构造函数/赋值操作员,将lvalue-reference带到const。
std ::移动(a)
和std :: move(b)
是rvalue(xvalue)表达式,它们可以绑定到lvalue-reference至const。您还可以检查
std :: is_move_move_move_constructible
::No. Even it doesn't have move constructor/assignment-operator, it has copy constructor/assignment-operator taking lvalue-reference to const.
std::move(a)
andstd::move(b)
are rvalue (xvalue) expressions and they could be bound to lvalue-reference to const.You might also check
std::is_move_constructible
:如果要使您的
entity
不可移动,请按删除添加移动组件和移动分配运算符:If you want to make your
Entity
non-movable, then add the move-constructor and move-assignment operator as deleted:不,不是。
实体
是可移动的。lvalue引用const可以绑定到rvalue。复制构造函数接受const参数的lVALUE参考。该参数可以绑定到
std ::移动(a)
rvalue参数。不完全。参考不是对象。命名任何名称的ID表达,包括RVALUE参考Varaible,都是LVALUE。
它是可移动的,因为它具有复制构造函数/分配运算符,并且没有删除移动构造函数/分配运算符。
No, it isn't.
Entity
is movable.Lvalue reference to const can be bound to an rvalue. The copy constructor accepts an lvalue reference to const parameter. That parameter can be bound to the
std::move(a)
rvalue argument.Not quite. References are not objects. An id-expression that names anything, including an rvalue reference varaible, is an lvalue.
It is movable because it has a copy constructor/assignment operator, and it doesn't have deleted move constructor/assignment operator.