名前空間
変種
操作

value initialization

提供: cppreference.com
< cpp‎ | language

 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
新しいオブジェクトにデフォルトの初期値を提供しています.
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.

目次

[編集] 構文

T object {}; (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 constructors
struct 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 0
    double f = double(); // non-class value-init, value is 0.0
    int* 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;
}

Output:

0 0 0 0 0
0 4199376

[編集] も参照してください