直接初期化
From cppreference.com
コンストラクタ引数の明示的なセットからオブジェクトを初期化します。
目次 |
[編集] 構文
T object ( arg );T object |
(1) | ||||||||
T object { arg }; |
(2) | (C++11以降) | |||||||
T ( other )T |
(3) | ||||||||
static_cast< T >( other ) |
(4) | ||||||||
new T( args, ... ) |
(5) | ||||||||
Class::Class() : member( args, ... ) { ... } |
(6) | ||||||||
[arg]() { ... } |
(7) | (C++11以降) | |||||||
[編集] 解説
直接初期化は以下の状況で実行されます。
1) 空でない括弧で囲まれた式のリスト または波括弧初期化子リスト(C++11以降) による初期化。
2) 非クラス型のオブジェクトを単一の波括弧で囲まれた初期化子で初期化する (注: クラス型や波括弧初期化子リストの他の使用については、リスト初期化を参照)(C++11以降)。
5) 初期化子を持つnew式による動的記憶域期間を持つオブジェクトの初期化。
6) コンストラクタ初期化子リストによる基底クラスまたは非静的メンバの初期化。
7) ラムダ式でコピーで捕捉された変数からのクロージャオブジェクトメンバの初期化。
直接初期化の効果は以下の通りです。
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