C++ – 初期化周りに関する仕様の纏め
Keyword
- list initializer
- initializer list
- brace initializer
- initialization
Question
- POD 型 Hoge を関数の引数に Hoge() として渡した場合はどういう仕様で 0 初期化されるのか?
- C++03 からの Value Initialization の仕様によってユーザ定義デフォルトコンストラクタを持っていないので zero-initialized される。
参考: Value Initialization
- C++03 からの Value Initialization の仕様によってユーザ定義デフォルトコンストラクタを持っていないので zero-initialized される。
Initialization
- どこで?
- declarator
- new
- function call
- どんな方法?
- (expression list)
eg. ClassKitty* pKitty(nullptr)
pKitty が declarator - = expression
eg. int a = 1;
a が declarator - {initializer list}
eg. std::string name{};
- (expression list)
- 種類
- Value initialization, e.g. std::string s{};
- T(), new T(), T{}, new T{} etc…
- 参考: value initialization
- Direct initialization, e.g. std::string s(“hello”);
- Copy initialization, e.g. std::string s = “hello”;
- List initialization, e.g. std::string s{‘a’, ‘b’, ‘c’};
- Aggregate initialization, e.g. char a[3] = {‘a’, ‘b’};
- Reference initialization, e.g. char& c = a[0];
- Value initialization, e.g. std::string s{};
declarator
- std::string name;
- std::string -> decl-specifier seq
- name -> declarator
参考
- https://en.cppreference.com/w/cpp/language/declarations
最下部 example
コメントを残す