-
Notifications
You must be signed in to change notification settings - Fork 75
{} list Initialization
list-initialization ({}) is a new form introduced in C++11, which can be used to initialize variables, including build-in types and user-defined types (classes).
Most build-in types have a default value (int => 0, bool => false, <pointer> => nullptr).
The benefit of using {}-list is that narrowing is not allowed.
int a; // uninitialized. int a {}; // a = 0; int b {1}; // b = 1; float c {a}; // Error: narrowing is not allowed
This can be divided into two subcases:
- Initialization without constructors
struct A { int a; }; // An aggregate class A object; // member a is uninitialized A object {}; // default initialization: a = 0 A object {1}; // aggregate initialization: a = 1
- Initialization with constructors
The list-initialization will call the corresponding constructor to construct the object. Aggregate initialization is not available in this case because classes with user-provided constructors are not aggregates.
struct A { A(int); A(); }; A object {}; // Call default ctor A() A object {1}; // Call A(1)
Things will be more complicated if the class has initializer-list constructor (a constructor that takes a single argument of std::initializer_list). The {}-list will be used as the argument in the initializer-list constructor.
If a class has serval constructors (default, initializer-list), the selected constructor follows two rules below:
- Prefer default constructor if either a default constructor or an initializer-list constructor can be invoked.
- Prefer initializer-list constructor if either a default constructor or an "ordinary" constructor can be invoked.
Rule 1 states that unlike non-empty {}-list, zero-element {}-list is a special case, which will be handled by a default constructor if present. This is reasonable -- because we use {}-list to initialize build-in type variables to default value, the same to classes.
struct A { A(); A(int); A(std::initializer_list<T>); }; A object (); // default ctor A object {}; // default ctor A object ({}); // initializer-list ctor A object {1}; // initializer-list ctor, not A(1) A object {1, 2}; // initializer-list ctor struct B { B(std::initializer_list<T>); }; B object; // Error, no default ctor B object {}; // initializer-list ctor as default ctor is not defined. B object ({}); // initializer-list ctor B obejct {1, 2}; // initializer-list ctor
- The C++ Programming language, chapter 6.3.5, 11.3, 17.3
LLVM/Clang
C/C++
- Get lower 32 bits from uint64
- How to unpack a std::tuple to a function with multiple arguments?
- {}-list Initialization
- Empty macro arguments
- 为什么能在函数中以by value方式返回unique_ptr?
- c++unsigned类型提升
- extern "C"
Linux
- ubuntu获取源码方法
- gcc/g++常用命令
- 浏览器导入安全证书
- ubuntu下宏包latex安装
- Bash Shell常用快捷键
- ubuntu把/tmp目录挂载到内存
- tar命令
- voyager12.04 apt-get install无法安装解决方法
- terminal shows git branch
- 编译GTK API源程序(附带pkg-config用法)
- ldconfig检查库是否存在
- Googletest Setup&Install
- Centos设置service开机自动启动
- CentOS create admin user
- 设置时区
- MySQL修改root密码
- MySQL常用命令
- Screen使用
- 环境变量
- Unity桌面环境的desktop文件
- zip和gzip文件区别
- Linux安全设置
Vim
- vim列编辑
- vim编辑二进制文件
- vim quickfix窗口
- Vim 批量操作
- Vim对多行重复操作
- mac下vim编译安装
- mac下vim taglist无效解决方法
- Vim 配置vim-airline
Tools
- gdb cgdb命令
- Source Insight添加.cc文件
- Source Insight快捷键
- GPT分区转MBR分区
- IRC工具Pidgin使用
- iTerm2 shortcuts
- MacOS shortcuts
- Compile/Run JUnitTest in Command Line
- Install Python2.7 on CentOS 6.4
- Install vmware tool on ubuntu server 12.04
- node-gyp Usage
- zsh中文乱码解决方法
- tmux快捷键
- 使用aria2突破百度云盘限速
- 配置 scheme编写环境
- How to list all available targets in ninja
Others
- CRLF换行符
- Git autocrlf设置
- Git reflog数据恢复命令
- how to migrate from SVN repo to Git repo
- Git submodule使用
- Git Pull强制更新
Chromium-Dev tips