C++ 中的返回值优化

代码:

cpp 复制代码
//
// Created by w on 2024/6/19.
//
#include <iostream>

using namespace std;

template<typename T>
class myClass {
public:
    myClass() {
        data = NULL;
        cout << "default construct" << endl;
    }

    myClass(const T &para) {
        data = new T(para);
        cout << "construct with para" << endl;
    }

    myClass(const myClass &other) {
        if (&other == this) {
            return;
        }

        data = new T(*other.data);
        cout << "copy construct" << endl;
    }


    myClass &operator=(const myClass &other) {
        if (&other == this) {
            return *this;
        }
        if (data != NULL) {
            delete data;
        }
        data = new T(*other.data);
        cout << "copy operator =" << endl;

        return *this;
    }

    myClass(myClass &&other) {
        if (&other == this) {
            return;
        }

        data = other.data;
        other.data = NULL;
        cout << "move construct" << endl;
    }


    myClass &operator=(myClass &&other) {
        if (&other == this) {

            return *this;
        }

        if (data != NULL) {
            delete data;
        }

        data = other.data;
        other.data = NULL;
        cout << "move operator =" << endl;

        return *this;
    }

    ~myClass() {
        if (data != NULL) {
            delete data;
        }
        cout << "destruct" << endl;
    }

    void print() {
        cout << *data << endl;
    }

private:
    T *data;
};

template<typename T>
myClass<T> f1() {
    return myClass<int>(1000);
}

template<typename T>
myClass<T> f2() {
    myClass<T> namedObj = myClass<int>(1000);
    return namedObj;
}

int main() {
    {
        myClass<int> obj = f1<int>();
    }
    cout << "############" << endl;
    {
        myClass<int> obj = f2<int>();
    }
}

直接运行输出(gcc默认开启了返回值优化):

construct with para

destruct

############

construct with para

move construct

destruct

destruct

关闭返回值优化输出:

g++ -fno-elide-constructors classDemo.cpp -std=c++11 && ./a.out

construct with para (构造匿名对象)

move construct (f1 用前面的匿名对象构造一个临时对象返回)

destruct (匿名对象析构)

move construct (obj使用临时对象move构造)

destruct (临时对象析构)

destruct (obj析构)

############

construct with para (构造匿名对象)

move construct (使用匿名对象构造具名对象)

destruct (匿名对象析构)

move construct (使用具名对象构造临时对象)

destruct (具名对象析构)

move construct (使用临时对象构造obj)

destruct (临时对象析构)

destruct (obj析构)

相关推荐
司铭鸿1 小时前
祖先关系的数学重构:从家谱到算法的思维跃迁
开发语言·数据结构·人工智能·算法·重构·c#·哈希算法
wavemap1 小时前
先到先得:免费订阅一年ChatGPT Go会员
开发语言·chatgpt·golang
sulikey2 小时前
C++ 四十年:一段跨越时代的语言旅程
c++·c++40周年
m***D2862 小时前
JavaScript在Node.js中的内存管理
开发语言·javascript·node.js
我叫张小白。2 小时前
JavaScript现代语法梳理:ES6+核心特性详解
开发语言·javascript·typescript·es6
-森屿安年-2 小时前
LeetCode 283. 移动零
开发语言·c++·算法·leetcode
寻找华年的锦瑟2 小时前
Qt-FFmpeg案例(0基础,包含环境配置)
开发语言·qt·ffmpeg
tanxiaomi2 小时前
Spring、Spring MVC 和 Spring Boot ,mybatis 相关面试题
java·开发语言·mybatis
浮尘笔记2 小时前
Go并发编程核心:Mutex和RWMutex的用法
开发语言·后端·golang
散峰而望2 小时前
C++数组(一)(算法竞赛)
c语言·开发语言·c++·算法·github