相关疑难解决方法(0)

为什么此move构造函数不称为“临时值”?

我有这段代码。我还希望d3通过move构造函数来创建,因为我传递了一个rvalue临时对象。

#include <iostream>

using namespace std;

struct Data {
    Data(): x(1)
    {
        cout << "constructor" << endl;
    }

    Data(const Data& original): x(2)
    {
        cout << "copy constructor" << endl;
    }

    Data(Data&& original): x(3)
    {
        cout << "move constructor" << endl;
    }

    int x;
};

int main() {
    Data d1; // constructor
    cout << "d1:" << d1.x << endl << endl;

    Data d2(d1); // copy constructor
    cout << "d2:" << d2.x << endl << endl;

    Data d3(Data{}); // move …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

5
推荐指数
0
解决办法
55
查看次数

标签 统计

c++ ×1

c++11 ×1