为什么不动的对象仍然是副本

发布于 2025-01-26 08:30:46 字数 1075 浏览 1 评论 0原文

考虑以下代码,实体对象是不可移动的。我知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女七分熟 2025-02-02 08:30:46

实体对象不可移动

。即使没有移动构造函数/分配操作员,它也具有复制构造函数/赋值操作员,将lvalue-reference带到const。 std ::移动(a)std :: move(b)是rvalue(xvalue)表达式,它们可以绑定到lvalue-reference至const。

您还可以检查 std :: is_move_move_move_constructible ::

类型没有移动构造函数,但带有一个复制构造函数,该构造函数接受const t&amp;参数,满足std :: is_move_constructible。

Entity object is non-movable

No. Even it doesn't have move constructor/assignment-operator, it has copy constructor/assignment-operator taking lvalue-reference to const. std::move(a) and std::move(b) are rvalue (xvalue) expressions and they could be bound to lvalue-reference to const.

You might also check std::is_move_constructible:

Types without a move constructor, but with a copy constructor that accepts const T& arguments, satisfy std::is_move_constructible.

第几種人 2025-02-02 08:30:46

如果要使您的entity不可移动,请按删除添加移动组件和移动分配运算符:

    Entity(Entity&& other) = delete;
    Entity& operator= (Entity&& other) = delete;

If you want to make your Entity non-movable, then add the move-constructor and move-assignment operator as deleted:

    Entity(Entity&& other) = delete;
    Entity& operator= (Entity&& other) = delete;
无语# 2025-02-02 08:30:46

考虑到以下代码,实体对象是不可移动的。

不,不是。 实体是可移动的。

但是我仍然让为什么语句自动temp = std :: move(a)

lvalue引用const可以绑定到rvalue。复制构造函数接受const参数的lVALUE参考。该参数可以绑定到std ::移动(a) rvalue参数。

我也知道rvalue参考变量仍然是一个lvalue对象。

不完全。参考不是对象。命名任何名称的ID表达,包括RVALUE参考Varaible,都是LVALUE。

实体是可移动的。确实是因为存在默认的移动构造函数/分配 - 操作器。

它是可移动的,因为它具有复制构造函数/分配运算符,并且没有删除移动构造函数/分配运算符。

Considering the following code, Entity object is non-movable.

No, it isn't. Entity is movable.

But I still confusing why statement auto temp = std::move(a)

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.

And I also know that rvalue reference variable is still a lvalue object.

Not quite. References are not objects. An id-expression that names anything, including an rvalue reference varaible, is an lvalue.

Entity is movable. Does is because there exists default move constructor/assignment-operator.

It is movable because it has a copy constructor/assignment operator, and it doesn't have deleted move constructor/assignment operator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文