You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关闭RVO/NRVO的C++14中对象返回及代码输出疑问

移动语义练习代码的执行过程解析

我正在学习移动语义,编写了如下练习代码:

#include <iostream>

using namespace std;

int one_int = 123;

class A {
public:
    int *a;

    A(int *ptr) : a(ptr) {
        cout << "In A's constructor..." << endl;
    }

    A(const A &other) {
        cout << "In A's copy constructor..." << endl;
        a = other.a;
    }

    A(A &&other) noexcept {
        cout << "In A's move constructor..." << endl;
        a = other.a;
        other.a = nullptr;
    }

    ~A() {
        cout << "In A's destructor..." << endl;
    }
};

A make_obj() {
    cout << "In make_obj..." << endl;
    return A(&one_int);
}

A make_obj_by_move(A &&source) {
    cout << "In make_obj_by_move..." << endl;
    return A(static_cast<A&&>(source));
}

A make_obj_by_copy(A source) {
    cout << "In make_obj_by_copy..." << endl;
    return A(source);
}

int main() {
    A obj2 = make_obj_by_move(make_obj());
    cout << endl;
    A obj1 = make_obj_by_copy(make_obj());
    cout << endl;
    // to ensure that the information printed when obj1 and obj2 are destroyed is separated from the above ones
    obj1.a = nullptr;
    obj2.a = nullptr;
    return 0;
}

我通过clang的-fno-elide-constructors选项关闭RVO/NRVO,使用-std=c++14标准编译运行,得到如下输出:

In make_obj...                                
In A's constructor...                   // make tmp obj
In A's move constructor...              // w/o RVO, need another construction to get return value?
In A's destructor...                    // destory the temp obj in make_obj scope?                
In make_obj_by_move...                
In A's move constructor...              // call the move constructor like expected? I guess by the position of "copy constructor" below
In A's move constructor...              // what happened here ???
In A's destructor...                    // destory which?            
In A's move constructor...              // w/o NRVO, need another construction to get obj1?
In A's destructor...                    // destory which?
In A's destructor...                    // destory which?

In make_obj...                            
In A's constructor...                   // make tmp obj
In A's move constructor...              // w/o RVO, need another construction to get return value?
In A's destructor...                    // destory the temp obj in make_obj scope?            
In A's move constructor...              // what happened here ???
In make_obj_by_copy...                
In A's copy constructor...              // call the copy constructor like expected
In A's move constructor...              // what happened here ???
In A's destructor...                    // destory which?            
In A's move constructor...              // w/o NRVO, need another construction to get obj2?
In A's destructor...                    // destory which?
In A's destructor...                    // destory which?
In A's destructor...                    // destory which?            

In A's destructor...                    // destory obj1/obj2
In A's destructor...                    // destory obj2/obj1

输出中出现了许多意料之外的移动构造和析构调用,我仅能解释部分内容,仍有多处无法理解,恳请帮忙解析具体执行过程。


内容的提问来源于stack exchange,提问作者Isuxiz Slidder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:07:16