[Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别
条款7 辨别使用()与{}创建对象的差别
基础知识
目前已知有如下的初始化方式:
int x();
int y = ;
int z{};
int z = {}; // the same as above
在以“=”初始化的过程中没有调用赋值运算,如下例所示:
Widget w1; // default ctor
Widget w2 = w1; // copy ctor
w1 = w2; // assignment, operator =
还可以用来初始化:
class Widget {
int x{}; // fine
int y = ; // fine
int z(); // error
};
std::atomic<int> ai1{}; // fine
std::atomic<int> ai2(); // fine
std::atomic<int> ai3 = ; // error
在构造函数中,()与{}在不含有的std::initializer_list的时候意义相同。但是如果出现std::initializer_list,则使用{}初始化语法的时候更倾向调用重载过后的函数。甚至那些拷贝和移动构造函数也会被劫持,例如:
class Widget {
public:
Widget(int i, bool b);
Widget(int i, double d);
Widget(std::initializer_list<long double> il);
operator float() const;
};
Widget w5(w4); // copy ctor
Widget w6{w4}; // w4 -> float -> initializer_list<long double>
std::initializer_list构造函数太强烈, 以致最优的匹配函数不是它,也会因缩窄匹配而错误:
Widget(std::initializer_list<bool> il);
Widget w{, 5.0}; // error, narrowing conversions
只有在不能通过转换匹配的情况下,编译器才查找原来的函数:
Widget(std::initializer_list<std::string> il);
Widget w{, 5.0}; // OK
在空值的情况下:
Widget w1; // default ctor
Widget w2{}; // default ctor
Widget w3(); // most vexing parse! function
总结
- {}初始化时最多使用的初始化语法,它不能进行缩窄转换(narrowing conversion),并且对一些令人烦恼的解析(most vexing parse)免疫
- 在构造函数重载解析过程中,{}初始化优先匹配std::initializer_list,即使其他构造函数更匹配
- 使用()或者{}的方式创建std::vector<numeric type>会有很大的不同
- 在模板中选择()或{}方式创建对象是一个挑战
[Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别的更多相关文章
- [Effective Modern C++] Item 6. Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句
条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> featu ...
- [Effective Modern C++] Item 5. Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto
条款5 相对显式类型声明,更倾向使用auto 基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型. template<typename It> void dwim(It ...
- [Effective Modern C++] Item 4. Know how to view deduced types - 知道如何看待推断出的类型
条款四 知道如何看待推断出的类型 基础知识 有三种方式可以知道类型推断的结果: IDE编辑器 编译器诊断 运行时输出 使用typeid()以及std::type_info::name可以获取变量的类型 ...
- [Effective Modern C++] Item 3. Understand decltype - 了解decltype
条款三 了解decltype 基础知识 提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪. 以下是一些简单的推断类型: ; // decltype(i) -> ...
- [Effective Modern C++] Item 2. Understand auto type deduction - 了解auto类型推断
条款二 了解auto类型推断 基础知识 除了一处例外,auto的类型推断与template一样.存在一个直接的从template类型推断到auto类型推断的映射 三类情况下的推断如下所示: // ca ...
- [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断
条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...
- Effective Modern C++ Item 27:重载universal references
假设有一个接收universal references的模板函数foo,定义如下: template<typename T> void foo(T&& t) { cout ...
- Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的
下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...
- Effective Modern C++ 42 Specific Ways to Improve Your Use of C++11 and C++14
Item 1: Understand template type deduction. Item 2: Understand auto type deduction. Item 3: Understa ...
随机推荐
- QT5.5实现串口通信
QT5.1以上版本自带QtSerialPort集成库,只要在头文件中集成 #include <QtSerialPort/QSerialPort> #include <QtSerial ...
- linux ar 命令的使用说明那个和例子[转]
用途说明 创建静态库.a文件.用C/C++开发程序时经常用到,但我很少单独在命令行中使用ar命令,一般写在makefile中,有时也会在shell脚 本中用到.关于Linux下的库文件.静态库.动态库 ...
- export-data.js
var timeBtnClick = (function() { function _todayClick() { $('.select-time .today').on('click', funct ...
- Yii 安装
// 安装 composer curl -s http://getcomposer.org/installer | php // 把 composer 添加到全局命令 mv composer.phar ...
- OS-MAC: An Efficient MAC Protocol for Spectrum-Agile Wireless Networks
IEEE TRANSACTIONS ON MOBILE COMPUTING, VOL. 7, NO. 8, AUGUST 2008 正如之前所说的,这是一个out-of-band local cove ...
- linux下mysql忘记root密码的解决方案
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...
- 认识IL代码---从开始到现在 <第二篇>
·IL代码分析方法 ·IL命令解析 ·.NET学习方法论 1.引言 自从『你必须知道.NET』系列开篇以来,受到大家很多的关注和支持,给予了anytao巨大的鼓励和动力.俱往昔,我发现很多的园友都把目 ...
- Jquery之家5个顶级Material Design框架
谷歌Material Design在如今的前端页面设计中非常流行.Material Design的设计风格向我们展示了一个简单而有内涵的现代UI设计方案. Material Design是如此的简洁美 ...
- Remove Element 解答
Question Given an array and a value, remove all instances of that value in place and return the new ...
- HTML5 Canvas Arc Tutorial
HTML5 Canvas Arc Tutorial HTML5 Canvas Arc Tutorial