C++编译器的返回值优化

文章讨论了C++编译器如何通过返回值优化(RVO)机制,避免对临时对象进行不必要的拷贝或移动操作,即使有移动构造函数,编译器也会直接构造临时对象到目标对象的内存中。
摘要由CSDN通过智能技术生成

C++编译器的返回值优化

在对右值概念的学习验证中发现函数构造的实例返回后不会被销毁(尽管已经跳出作用域),而是直接被使用

上代码

#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <random>
#include <utility>
#include <string>

using namespace std;

class MoveOrCopy {
private:
    int a;
public:
    MoveOrCopy() : a(0) {
        printf("default consult: %p\n", this);
    }

    MoveOrCopy(const MoveOrCopy &m) : a(m.a) {
        printf("copy consult is called: %p\n", this);
    }

    MoveOrCopy(MoveOrCopy &&m) noexcept: a(m.a) {
        printf("move consult is called: %p\n", this);
    }

    virtual ~MoveOrCopy() {
        printf("MoveOrCopy %p:%d is deleted\n", this, this->a);
    }

    MoveOrCopy &operator=(const MoveOrCopy &m) {
        this->a = m.a;
        printf("copy valued is called\n");
    }
};

MoveOrCopy create() {
    return MoveOrCopy();
}

MoveOrCopy createWithLocal() {
    MoveOrCopy t = MoveOrCopy();
    return t;
}

MoveOrCopy createCondition(bool b = false) {
    return b ? MoveOrCopy() : MoveOrCopy();
}

int main() {
    printf("d:\n");
    MoveOrCopy d = create();
    printf("e:\n");
    MoveOrCopy e = createWithLocal();
    printf("f:\n");
    MoveOrCopy f = createCondition();
    return 0;
}

运行结果

d:
default consult: 000000D4B12FFB48
e:
default consult: 000000D4B12FFAD8
move consult is called: 000000D4B12FFB38
MoveOrCopy 000000D4B12FFAD8:0 is deleted
f:
default consult: 000000D4B12FFAC8
move consult is called: 000000D4B12FFB28
MoveOrCopy 000000D4B12FFAC8:0 is deleted
MoveOrCopy 000000D4B12FFB28:0 is deleted
MoveOrCopy 000000D4B12FFB38:0 is deleted
MoveOrCopy 000000D4B12FFB48:0 is deleted

解释

编译器对返回值进行了优化,将临时对象直接构造在 c 对象的空间中,避免了不必要的拷贝或移动操作。

这种优化被称为返回值优化(Return Value Optimization,RVO),它是 C++ 编译器的一种常见优化手段,用于避免不必要的对象拷贝或移动。在这种情况下,即使定义了移动构造函数,编译器也不会调用移动构造函数,而是直接将临时对象构造在 c 对象的空间中。

参考连接

1: 返回值优化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值