直接初始化
来自 cppreference.cn
使用一组显式构造函数参数初始化对象。
目录 |
[编辑] 语法
T 对象 ( arg ); T 对象 |
(1) | ||||||||
T 对象 { arg }; |
(2) | (C++11 起) | |||||||
T ( other ) T |
(3) | ||||||||
static_cast< T >( other ) |
(4) | ||||||||
new T( args, ... ) |
(5) | ||||||||
类:: 类() : 成员( args, ... ) { ... } |
(6) | ||||||||
[ arg]() { ... } |
(7) | (C++11 起) | |||||||
[编辑] 解释
直接初始化在以下情况发生
1) 使用非空括号表达式列表 或花括号初始化列表(C++11 起) 进行初始化。
2) 使用单个花括号初始化器初始化非类类型对象 (注意:对于类类型和花括号初始化列表的其他用途,请参见列表初始化)(C++11 起)。
5) 通过带初始化器的 new-expression 初始化具有动态存储期的对象。
6) 通过构造函数初始化列表初始化基类或非静态成员。
7) 从 lambda 表达式中通过拷贝捕获的变量初始化闭包对象成员。
直接初始化的效果是
- 如果
T
是数组类型,
|
(C++20 前) |
struct A { explicit A(int i = 0) {} }; A a[2](A(1)); // OK: initializes a[0] with A(1) and a[1] with A() A b[2]{A(1)}; // error: implicit copy-list-initialization of b[1] // from {} selected explicit constructor |
(C++20 起) |
- 如果
T
是类类型,
(C++17 起) |
- 检查
T
的构造函数,并通过重载决议选择最佳匹配。然后调用该构造函数来初始化对象。
- 检查
struct B { int a; int&& r; }; int f(); int n = 10; B b1{1, f()}; // OK, lifetime is extended B b2(1, f()); // well-formed, but dangling reference B b3{1.0, 1}; // error: narrowing conversion B b4(1.0, 1); // well-formed, but dangling reference B b5(1.0, std::move(n)); // OK |
(C++20 起) |
- 否则,如果
T
是非类类型但源类型是类类型,则检查源类型及其基类的转换函数(如果有),并通过重载决议选择最佳匹配。然后使用选定的用户定义转换将初始化器表达式转换为正在初始化的对象。 - 否则,如果
T
是 bool 且源类型是 std::nullptr_t,则初始化对象的值为 false。 - 否则,如果需要,使用标准转换将other的值转换为
T
的 cv-非限定版本,并且正在初始化的对象的初始值是(可能已转换的)值。
[编辑] 注意
直接初始化比拷贝初始化更宽容:拷贝初始化只考虑非explicit构造函数和非 explicit 用户定义转换函数,而直接初始化考虑所有构造函数和所有用户定义转换函数。
在使用直接初始化语法 (1)(带圆括号)的变量声明与函数声明之间发生歧义时,编译器总是选择函数声明。这种消歧规则有时是反直觉的,被称为最令人烦恼的解析。
运行此代码
#include <fstream> #include <iterator> #include <string> int main() { std::ifstream file("data.txt"); // The following is a function declaration: std::string foo1(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()); // It declares a function called foo1, whose return type is std::string, // first parameter has type std::istreambuf_iterator<char> and the name "file", // second parameter has no name and has type std::istreambuf_iterator<char>(), // which is rewritten to function pointer type std::istreambuf_iterator<char>(*)() // Pre-C++11 fix (to declare a variable) - add extra parentheses around one // of the arguments: std::string str1((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); // Post-C++11 fix (to declare a variable) - use list-initialization for any // of the arguments: std::string str2(std::istreambuf_iterator<char>{file}, {}); }
[编辑] 示例
运行此代码
#include <iostream> #include <memory> #include <string> struct Foo { int mem; explicit Foo(int n) : mem(n) {} }; int main() { std::string s1("test"); // constructor from const char* std::string s2(10, 'a'); std::unique_ptr<int> p(new int(1)); // OK: explicit constructors allowed // std::unique_ptr<int> p = new int(1); // error: constructor is explicit Foo f(2); // f is direct-initialized: // constructor parameter n is copy-initialized from the rvalue 2 // f.mem is direct-initialized from the parameter n // Foo f2 = 2; // error: constructor is explicit std::cout << s1 << ' ' << s2 << ' ' << *p << ' ' << f.mem << '\n'; }
输出
test aaaaaaaaaa 1 2