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++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

C++实现获取本机MAC地址与IP地址

《C++实现获取本机MAC地址与IP地址》这篇文章主要为大家详细介绍了C++实现获取本机MAC地址与IP地址的两种方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实际工作中,项目上常常需要获取本机的IP地址和MAC地址,在此使用两种方案获取1.MFC中获取IP和MAC地址获取

C/C++通过IP获取局域网网卡MAC地址

《C/C++通过IP获取局域网网卡MAC地址》这篇文章主要为大家详细介绍了C++如何通过Win32API函数SendARP从IP地址获取局域网内网卡的MAC地址,感兴趣的小伙伴可以跟随小编一起学习一下... C/C++通过IP获取局域网网卡MAC地址通过win32 SendARP获取MAC地址代码#i

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

C++中实现调试日志输出

《C++中实现调试日志输出》在C++编程中,调试日志对于定位问题和优化代码至关重要,本文将介绍几种常用的调试日志输出方法,并教你如何在日志中添加时间戳,希望对大家有所帮助... 目录1. 使用 #ifdef _DEBUG 宏2. 加入时间戳:精确到毫秒3.Windows 和 MFC 中的调试日志方法MFC

五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布

《五大特性引领创新!深度操作系统deepin25Preview预览版发布》今日,深度操作系统正式推出deepin25Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Tr... 深度操作系统今日发布了 deepin 25 Preview,新版本囊括五大特性:磐石系统、全新 DDE、Tree