C++11新增了列表初始化的概念。c++
在C++11中能够直接在变量名后面加上初始化列表来进行对象的初始化。数组
struct A { public: A(int) {} private: A(const A&) {} }; int main() { A a(123); A b = 123; // error A c = { 123 }; A d{123}; // c++11 int e = {123}; int f{123}; // c++11 return 0; }
列表初始化也能够用在函数的返回值上ide
std::vector<int> func() { return {}; }
列表初始化的一些规则:函数
首先说下聚合类型能够进行直接列表初始化,这里须要了解什么是聚合类型:优化
类型是一个类,且知足如下条件:.net
struct A { int a; int b; int c; A(int, int){} }; int main() { A a{1, 2, 3};// error,A有自定义的构造函数,不能列表初始化 }
上述代码类A不是聚合类型,没法进行列表初始化,必须以自定义的构造函数来构造对象。c++11
struct A { int a; int b; virtual void func() {} // 含有虚函数,不是聚合类 }; struct Base {}; struct B : public Base { // 有基类,不是聚合类 int a; int b; }; struct C { int a; int b = 10; // 有等号初始化,不是聚合类 }; struct D { int a; int b; private: int c; // 含有私有的非静态数据成员,不是聚合类 }; struct E { int a; int b; E() : a(0), b(0) {} // 含有默认成员初始化器,不是聚合类 };
上面列举了一些不是聚合类的例子,对于一个聚合类型,使用列表初始化至关于对其中的每一个元素分别赋值;对于非聚合类型,须要先自定义一个对应的构造函数,此时列表初始化将调用相应的构造函数。code
std::initializer_list对象
咱们平时开发使用STL过程当中可能发现它的初始化列表能够是任意长度,你们有没有想过它是怎么实现的呢,答案是std::initializer_list,看下面这段示例代码:blog
struct CustomVec { std::vector<int> data; CustomVec(std::initializer_list<int> list) { for (auto iter = list.begin(); iter != list.end(); ++iter) { data.push_back(*iter); } } };
我想经过上面这段代码你们可能已经知道STL是如何实现的任意长度初始化了吧,这个std::initializer_list其实也能够做为函数参数。
注意:std::initializer_list<T>,它能够接收任意长度的初始化列表,可是里面必须是相同类型T,或者均可以转换为T。
列表初始化的好处
我的认为列表初始化的好处以下:
什么是类型窄化,列表初始化经过禁止下列转换,对隐式转化加以限制:
示例:
int main() { int a = 1.2; // ok int b = {1.2}; // error float c = 1e70; // ok float d = {1e70}; // error float e = (unsigned long long)-1; // ok float f = {(unsigned long long)-1}; // error float g = (unsigned long long)1; // ok float h = {(unsigned long long)1}; // ok const int i = 1000; const int j = 2; char k = i; // ok char l = {i}; // error char m = j; // ok char m = {j}; // ok,由于是const类型,这里若是去掉const属性,也会报错 }
打印以下:
test.cc:24:17: error: narrowing conversion of ‘1.2e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing] int b = {1.2}; ^ test.cc:30:38: error: narrowing conversion of ‘18446744073709551615’ from ‘long long unsigned int’ to ‘float’ inside { } [-Wnarrowing] float f = {(unsigned long long)-1}; ^ test.cc:36:14: warning: overflow in implicit constant conversion [-Woverflow] char k = i; ^ test.cc:37:16: error: narrowing conversion of ‘1000’ from ‘int’ to ‘char’ inside { } [-Wnarrowing] char l = {i};
关于列表初始化的全部知识点就是这些,若有遗漏或者遗漏的你们积极留言哈,请持续关注~
《深刻应用c++11:代码优化与工程级应用》
https://blog.csdn.net/hailong...
https://zh.cppreference.com/w...
https://zh.cppreference.com/w...
https://zh.cppreference.com/w...
更多文章,请关注个人V X 公 主 号:程序喵大人,欢迎交流。