return{}的含义 - C++11新特性

2024-06-21 17:38
文章标签 c++ 特性 return 含义

本文主要是介绍return{}的含义 - C++11新特性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  之前有小伙伴在刷题过程中,遇到 return{} 懵逼了,其实这个是C++11的新特性,本文将介绍return{}的用法,如果有不足欢迎大佬斧正(>人<;)~
  我们先来看看比较官方的details

  • If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.

  • Otherwise, if T is an aggregate type, aggregate initialization is performed.

  • Otherwise, if T is a specialization of std::initializer_list, the T object is direct-initialized or copy-initialized, depending on
    context, from the braced-init-list.

  • Otherwise, the constructors of T are considered, in two phases:
     - All constructors that take std::initializer_list as the only
    argument, or as the first argument if the remaining arguments have
    default values, are examined, and matched by overload resolution
    against a single argument of type std::initializer_list
     - If the previous stage does not produce a match, all constructors of T
    participate in overload resolution against the set of arguments that
    consists of the elements of the braced-init-list, with the restriction
    that only non-narrowing conversions are allowed. If this stage
    produces an explicit constructor as the best match for a
    copy-list-initialization, compilation fails (note, in simple
    copy-initialization, explicit constructors are not considered at all).

  • Otherwise (if T is not a class type), if the braced-init-list has only one element and either T isn’t a reference type or is a reference
    type that is compatible with the type of the element, T is
    direct-initialized (in direct-list-initialization) or copy-initialized
    (in copy-list-initialization), except that narrowing conversions are
    not allowed.

  • Otherwise, if T is a reference type that isn’t compatible with the type of the element. (this fails if the reference is a non-const
    lvalue reference)

  • Otherwise, if the braced-init-list has no elements, T is value-initialized.

  看了解释后,归结就是 “返回用大括弧初始化的该函数对象(返回的类型和函数类型一样)” 举个例子后就能明白了
C++11之前, 你想返回一个string的函数,你可能会这样写:

string get_string() {return string();
}

C++11之后,可以这样写:

std::string get_string() {return {};   // 即返回这个函数所构造出来的对象
}这里再举个例子:
vector<int> twoSum(vector<int>& nums, int target) {//...经过一系列操作后构造好了nums这个vector数组if(target > 10) return {};       //返回一个空的vector<int>对象else if(target < 5) return nums;    //直接返回nums作为对象else return {1, 2, 3};    //直接构造好vector<int>并将这个对象返回
}

这篇关于return{}的含义 - C++11新特性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

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

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(