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析构)

相关推荐
weixin_4723394643 分钟前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击1 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue3 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
tan180°3 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
m0_555762903 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
浪裡遊4 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
彭祥.5 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
lzb_kkk5 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼5 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
简佐义的博客6 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang