Modern C++——函数参数类型的分类和使用

2024-08-28 08:20

本文主要是介绍Modern C++——函数参数类型的分类和使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

大纲

  • 基本定义
  • 值类型
  • 左值引用
    • 非常量左值引用
    • 常量左值引用
  • 右值引用
  • 总结

在C++中,函数参数主要有两种方式:值类型和引用类型。其中引用类型又分为:左值引用和C++11引入的右值引用。下面我们分别对其进行介绍。

基本定义

要弄清楚这些概念,我们先要知道什么是左值,什么是右值。

  • 左值是指在内存中有确定存储地址的对象,可以在表达式中出现在赋值操作符的左侧。
  • 右值是指在表达式中没有持久存储地址的临时对象,通常出现在赋值操作符的右侧。

比如下面的代码中,a是左值,10是右值。

int a = 10;

值类型

值类型是最基本的参数类型。

在函数被调用时:

  • 如果实参是左值,它就会被复制一份到形参中(调用复制构造函数)。这意味着在函数体内对形参的任何修改都不会影响到实参本身。值传递适用于基本数据类型(如int, float等)和对象的小型拷贝开销可以接受的情况。
  • 如果实参是右值引用,则右值引用的对象会触发形参的移动构造函数。这减少了不必要的资源分配和释放,提高了性能。

我们设计了一个Custom类,它会在各个构造函数中打印this指针的值和相关标记信息,用于辅助我们查看和分析后续研究代码。

class Custom {
public:Custom(int value) : value(value) {std::cout << "Constructor Custom(" << value << "). this = " << this << std::endl;}Custom(const Custom& custom) : value(custom.value) {std::cout << "Copy constructor Custom(" << custom.value << "). this = " << this << " from " << &custom << std::endl;}Custom& operator=(const Custom& custom) {value = custom.value;std::cout << "Copy assignment operator Custom(" << custom.value << "). this = " << this << " from " << &custom << std::endl;return *this;}Custom(Custom&& custom) : value(custom.value) {std::cout << "Move constructor Custom(" << custom.value << "). this = " << this << " from " << &custom << std::endl;custom.value = 0;}Custom& operator=(Custom&& custom) {value = custom.value;std::cout << "Move assignment operator Custom(" << custom.value << "). this = " << this << " from " << &custom << std::endl;return *this;}~Custom() {std::cout << "Destructor Custom(" << value << "). this = " << this << std::endl;}friend std::ostream& operator<<(std::ostream& os, const Custom& custom) {os << custom.value;return os;}public:int get_value() const {return value;}void set_value(int value) {this->value = value;}private:int value;
};

下面的函数func的参数是值类型。

void func(Custom custom) {std::cout << "func(Custom custom) custom = " << custom << " from " << &custom << std::endl;
}

然后分别给func传入左值和右值,观察其执行过程。

int main(int argc, char* argv[]) {Custom custom(1);std::cout << "build custom in main" << std::endl;std::cout << "call func(custom)" << std::endl;func(custom);std::cout << "call func(std::move(custom))" << std::endl;func(std::move(custom));std::cout << "main end" << std::endl;return 0;
}

可以看到给值类型传入左值,会触发拷贝构造函数(过程2);给值类型传入右值引用,会触发移动构造函数(过程3)。
在这里插入图片描述

左值引用

左值引用(Lvalue Reference)是C++中的一种引用类型,用于引用一个左值(Lvalue)。左值引用的语法是使用单个 & 符号。

非常量左值引用

如果函数的参数是左值引用,这意味着函数直接操作的是原变量的内存地址,因此对形参的修改会反映到实参上。使用左值引用可以避免大型对象或结构体的复制开销,提高效率。

void func(Custom& custom) {std::cout << "func(Custom custom) custom = " << custom << " from " << &custom << std::endl;
}int main(int argc, char* argv[]) {Custom custom(1);Custom& custom2 = custom;const Custom& custom3 = custom;func(custom);func(custom2);// func(custom3);  // error// func(std::move(custom)); // error: cannot bind non-const lvalue reference of type 'Custom&' to an rvalue of type 'Custom'std::cout << "main custom = " << custom << " from " << &custom << std::endl;return 0;
}

需要注意的是:非常量左值引用(T&)只能绑定到左值,不能绑定到右值。

常量左值引用

有些时候,我们仅仅希望避免复制操作,而不希望函数修改原变量的值,这样我们就需要使用常量左值引用。

void func_const(const Custom& custom) {std::cout << "func_const(const Custom& custom) custom = " << custom << " from " << &custom << std::endl;
}int main(int argc, char* argv[]) {Custom custom(1);Custom& custom2 = custom;const Custom& custom3 = custom;func_const(custom);func_const(custom2);func_const(custom3);func_const(std::move(custom));  // const lvalue reference can bind to rvaluestd::cout << "main custom = " << custom << " from " << &custom << std::endl;return 0;
}

我们给常量左值引用形参传递值、左值引用、常量左值引用、右值引用都是可以的,其执行效果是一样的。

在这里插入图片描述

需要注意的是,传递右值引用并不出触发Custom的移动构造函数执行。我们不要被std::move这个函数名给骗了,它不会进行任何的“移动”操作。它只是将一个左值/右值强制转换成右值。

    constexpr typename std::remove_reference<_Tp>::type&&move(_Tp&& __t) noexcept{ return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }

右值引用

右值引用(Rvalue Reference)是C++11引入的一种引用类型,用于引用一个右值(Rvalue)。右值引用的语法是使用双 && 符号。

右值引用通常与移动语义结合使用,用于优化资源的转移,如动态分配的内存、文件句柄等。通过右值引用传递,我们可以避免在函数调用中不必要的资源复制,而是直接“移动”资源给函数内的变量。

void func(Custom&& custom) {std::cout << "func(Custom custom) custom = " << custom << " from " << &custom << std::endl;
}void func_const(const Custom&& custom) {std::cout << "func(Custom custom) custom = " << custom << " from " << &custom << std::endl;
}int main(int argc, char* argv[]) {Custom custom(1);Custom& custom2 = custom;const Custom& custom3 = custom;// func(custom); // error: cannot bind rvalue reference of type 'Custom&&' to lvalue of type 'Custom'// func(custom2); // error// func(custom3); // errorfunc(std::move(custom));// func_const(custom); // error: cannot bind rvalue reference of type 'const Custom&&' to lvalue of type 'Custom'// func_const(custom2); // error// func_const(custom3); // errorfunc_const(std::move(custom));std::cout << "main end" << std::endl;return 0;
}

右值引用形参只能接受右值引用实参。

总结

  • 当右值引用需要做复制操作时,会触发移动构造函数。
  • 值类型形参会触发复制操作,而引用类型不会触发复制操作。所以如果希望触发移动构造函数,需要将参数形参设计为值类型。

这篇关于Modern C++——函数参数类型的分类和使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1114233

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB