简体   繁体   English

为什么这个C ++ 0x代码不会调用移动构造函数?

[英]Why doesn't this C++0x code call the move constructor?

For some reason, the following code never calls Event::Event(Event&& e) 出于某种原因,以下代码从不调用Event::Event(Event&& e)

Event a;
Event b;
Event temp;
temp = move(a);
a = move(b);
b = move(temp);

why not? 为什么不?

Using std::swap calls it once. 使用std::swap调用一次。

class Event {
public:
    Event(): myTime(0.0), myNode(NULL) {}
    Event(fpreal t, Node* n);
    Event(Event&& other);
    Event(Event const& other) = delete;
    ~Event();

    bool                operator<(Event const& other) const { return myTime < other.myTime; }
    bool                operator>(Event const& other) const { return myTime > other.myTime; }
    fpreal              getTime() const { return myTime; }
    void                setTime(fpreal time) { myTime = time; }
    Node*               getNode() const { return myNode; }

private:
    fpreal              myTime;
    Node*               myNode;
};

You are not using the move constructor. 您没有使用移动构造函数。 I think swap is implemented something like this 我认为交换是这样实现的

Event a;
Event b;

Event temp(move(a)); // this one wants to use a move constructor
a = move(b);
b = move(temp);

You want to use the move assignment operator, which doesn't exist in your code, so it falls back to the copy assignment operator. 您希望使用移动赋值运算符,该运算符在代码中不存在,因此它将回退到复制赋值运算符。

Your code has two potential locations for where one may expect the move constructor to get called (but it doesn't): 您的代码有两个可能的位置,可以指望移动构造函数被调用(但它没有):

1) calling std::move 1)调用std :: move
2) during assignment. 2)在任职期间。

Regarding 1), std::move does a simple cast - it does not create an object from a copy - if it did then the move constructor might get invoked by it, but since it does a simple rvalue cast it doesn't get invoked. 关于1),std :: move执行一个简单的转换 - 它不会从副本创建一个对象 - 如果它确实,那么移动构造函数可能会被它调用,但是因为它执行了一个简单的右值转换,所以它不会被调用。 The definition of std::move is similar to static_cast<Event&&>(temp) . std :: move的定义类似于static_cast<Event&&>(temp)

Regarding 2), Initialization and assignment are two entirely different operations (even though some forms of initialization use the '=' symbol). 关于2),初始化和赋值是两个完全不同的操作(即使某些形式的初始化使用'='符号)。 Your code does assignment and therefore uses the default assignment operator which is declared to accept a const lvalue reference. 您的代码执行赋值,因此使用声明为接受const左值引用的默认赋值运算符。 Since you never initialize one event object with another, you won't see your move constructor get invoked. 由于您永远不会使用另一个事件对象初始化一个事件对象,因此您不会看到您的移动构造函数被调用。 If you declared a move assignment operator: Event& operator=(Event&& other) , then your current code would invoke it or if you wrote: Event a; Event tmp = move(a); 如果你声明了一个移动赋值运算符: Event& operator=(Event&& other) ,那么你当前的代码会调用它,或者如果你写了: Event a; Event tmp = move(a); Event a; Event tmp = move(a); your move constructor, as written, would get invoked. 你的移动构造函数,如所写的,将被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM