The Wayback Machine - https://web.archive.org/web/20180227220854/http://ja.cppreference.com:80/w/cpp/language/value_initialization
名前空間
変種
操作

value initialization

提供: cppreference.com
< cpp‎ | language

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外仕様(廃止予定)
noexcept 指定子(C++11)
例外
名前空間
指定子
decltype(C++11)
auto(C++11)
alignas(C++11)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点
文字 - 文字列 - nullptr(C++11)
ユーザ定義(C++11)
ユーティリティ
アトリビュート(C++11)
typedef 宣言
型エイリアス宣言(C++11)
キャスト
暗黙の変換 - 明示的な変換
static_cast - dynamic_cast
const_cast - reinterpret_cast
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
テンプレート
その他
 
新しいオブジェクトにデフォルトの初期値を提供しています.
Original:
Provides the default initial value to a new object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集]構文

Tobject{}; (1) (C++11およびそれ以降)
T();

T{};

(2)
(C++11およびそれ以降)
new T();

new T{};

(3)
(C++11およびそれ以降)

[編集]説明

値の初期化は、次の3つの状況で実行されます
Original:
Value initialization is performed in three situations:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
という名前の変数に、(自動、静的、またはスレッドローカル)括弧のペアで構成される初期化子を使用して宣言されたとき。 (C++11およびそれ以降)
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces. (C++11およびそれ以降)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
無名の一時オブジェクトは括弧または中括弧の空の対からなる初期化子を使用して作成されたときに.
Original:
when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
動的記憶域期間を持つオブジェクトは、括弧または中括弧の空の対からなる初期化子を使用して新しい式によって作成されたときに.
Original:
when an object with dynamic storage duration is created by a new-expression with the initializer consisting of an empty pair of parentheses or braces.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
値の初期化の効果は、次のとおりです
Original:
The effects of value initialization are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Tは、任意のユーザーが提供するコンストラクタなしで非組合クラス型である場合、そのオブジェクトはゼロで初期化れ、その後、暗黙的に宣言されたデフォルトのコンストラクタが(それは些細な場合を除きます)と呼ばれています
    Original:
    If T is an non-union class type without any user-provided constructors, then the object is ゼロで初期化 and then the implicitly-declared default constructor is called (unless it's trivial)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Tが配列型である場合は、配列の各要素の値は初期化されます
    Original:
    If T is an array type, each element of the array is value-initialized
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • それ以外の場合は、オブジェクトはゼロで初期化され.
    Original:
    Otherwise, the object is zero-initialized.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[編集]ノート

構文T object();は、オブジェクトを初期化するのではなく、引数および戻りTを取らない関数を宣言します。ほとんどのコンパイラは、この場合にはコピーを最適化..:C + + 11の値が初期化してから、一時的なオブジェクトをコピーするには、初期化する、T object = T();た前という名前の変数を値に初期化する方法
Original:
The syntax T object(); does not initialize an object; it declares a function that takes no arguments and returns T. The way to value-initialize a named variable before C++11 was T object = T();, which value-initializes a temporary and then copy-initializes the object: most compilers optimize out the copy in this case.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
参照は値初期化することはできません.
Original:
References cannot be value-initialized.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シングルstd::vector引数を指定して場合やstd::listへの呼び出しによって成長し、構築時に、すべての標準的なコンテナ(size_typeresize()など)それらの要素を値初期化.
Original:
All standard containers (std::vector, std::list, etc) value-initialize their elements when constructed with a single size_type argument or when grown by a call to resize().
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

#include <string>#include <vector>#include <iostream>   struct T1 {int mem1;std::string mem2;};// no constructorsstruct T2 {int mem1;std::string mem2; T2(const T2&){}// a constructor, but no default};struct T3 {int mem1;std::string mem2; T3(){}// user-provided default ctor};   std::string s{};// calls default ctor, the value is "" (empty string)int main(){int n{};// non-class value-initialization, value is 0double f =double();// non-class value-init, value is 0.0int* a = new int[10]();// array of 10 zeroes   T1 t1{};// no ctors: zero-initialized// t1.mem1 is zero-initialized// t1.mem2 is default-initialized// T2 t2{}; // error: has a ctor, but no default ctor T3 t3{};// user-defined default ctor:// t3.mem1 is default-initialized (the value is indeterminate)// t3.mem2 is default-initialized   std::vector<int> v(3);// value-initializes three ints   std::cout<< s.size()<<' '<< n <<' '<< f <<' '<< a[9]<<' '<< v[2]<<'\n';std::cout<< t1.mem1<<' '<< t3.mem1<<'\n'; delete[] a;}

出力:

0 0 0 0 0 0 4199376

[編集]参照

close