名前空間
変種
操作

「cpp/language/constant initialization」の版間の差分

提供: cppreference.com
< cpp‎ | language
(1版:Translate from the English version)
 
(2人の利用者による、間の6版が非表示)
1行: 1行:
{{tr_note}}
+
{{title|}}
{{title|constant initialization}}
+
{{cpp/language/navbar}}
{{cpp/language/navbar}}
+
 
Sets the initial values of the static constants
+
  
 
===構文===
 
===構文===
{{sdcl list begin}}
+
 
{{sdcl list item | num=1 | {{ttb|static}} {{sparam|T}} {{ttb|&}} {{sparam|ref}} {{ttb|{{=}} {{sparam|constexpr}};}}}}
+
{{begin}}
{{sdcl list item | num=2 | {{ttb|static}} {{sparam|T}} {{sparam| object}} {{ttb|{{=}} {{sparam|constexpr}};}}}}
+
{{| num=1 | {{ttb|static}} {{|T}} {{ttb|&}} {{|ref}} {{ttb|{{=}} {{|constexpr}};}}}}
{{sdcl list end}}
+
{{| num=2 | {{ttb|static}} {{|T}} {{| object}} {{ttb|{{=}} {{|constexpr}};}}}}
 +
{{end}}
  
 
===説明===
 
===説明===
Constant initialization is performed after zero initialization of the static and thread-local objects and before all other initialization. Only the following variables are constant initialized:
 
@1@ Static or thread-local references, if it is bound to static lvalue or to a temporary. and if every expression (including implicit conversions) in [[cpp/language/reference_initialization|the initializer]] of the reference is a [[cpp/language/constant_expression|定数式]].
 
@2@ Static or thread-local object of class type that is initialized by a constructor call, if the constructor is {{c|constexpr}} and all constructor arguments (including implicit conversions) are constant expressions, and if the initializers in the constructor's initializer list and the brace-or-equal initializers of the class memebers only contain constant expressions.
 
@3@ Static or thread-local object (not necessarily of class type), that is not initialized by a constructor call, if every expression in its initializer is a constant expression.
 
  
The effects of constant initialization are the same as the effects of the corresponding initialization, except that it's guaranteed that it is complete before any other initialization of a static or thread-local object begins, and it may be performed at compile time.
+
 +
 +
constant
 +
initialization
  
The object that was initialized by constant initialization can be used in constant expressions, e.g. in an array declaration.
+
  
 
===ノート===
 
===ノート===
The compiler is permitted to initialize other static and thread-local objects using constant initialization, if it can guarantee that the value would be the same as if the standard order of initialization was followed. Such objects can be used in constant expressions, but this use is not portable.
+
 +
 
 +
..
  
 
===例===
 
===例===
 +
 
{{example
 
{{example
 
  |
 
  |
33行: 36行:
 
     static const int c;
 
     static const int c;
 
};
 
};
const int d = 10 * S::c; // not a constant expression: S::c has no preceding
+
const int d = 10 * S::c; // S::c
                         // initializer, this initialization happens after const
+
                         // const
const int S::c = 5;     // constant initialization, guaranteed to happen first
+
const int S::c = 5; //
 
int main()
 
int main()
 
{
 
{
 
     std::cout << "d = " << d << '\n';
 
     std::cout << "d = " << d << '\n';
     std::array<int, S::c> a1; // OK: S::c is a constant expression
+
     std::array<int, S::c> a1; // S::c
//  std::array<int, d> a2;    // error: d is not a constant expression
+
//  std::array<int, d> a2;    // d
 
}
 
}
 
  | output=
 
  | output=
46行: 49行:
 
}}
 
}}
  
===も参照してください===
+
===
* [[cpp/language/constexpr|constexprの指定子]]
+
 +
 +
 +
 
 +
===
 +
* [[cpp/language/constexpr|]]
 
* [[cpp/language/zero_initialization|ゼロ初期化]]
 
* [[cpp/language/zero_initialization|ゼロ初期化]]
 +
 +

2018年9月26日 (水) 01:52時点における最新版

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外指定 (C++20未満)
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
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
テンプレート
その他
 
 

静的変数の初期値をコンパイル時定数に初期化します。

目次

[編集] 構文

static T & ref = constexpr; (1)
static T object = constexpr; (2)

[編集] 説明

定数初期化は静的およびスレッドローカルなオブジェクトのゼロ初期化の後に (C++14未満)の代わりに (C++14以上)他のすべての初期化の前に行われます。 以下の変数のみが定数初期化されます。

1) 静的またはスレッドローカルな参照で、静的な glvalue、一時オブジェクト (またはその部分オブジェクト)、または関数に束縛され、その参照の初期化子内のすべての式 (暗黙の変換を含みます) が定数式の場合。
2) コンストラクタ呼び出しによって初期化されるクラス型の静的またはスレッドローカルなオブジェクトで、そのコンストラクタが constexpr であり、すべてのコンストラクタ引数 (暗黙の変換を含みます) が定数式であり、コンストラクタの初期化子リスト内の初期化子およびクラスメンバの波括弧または等号の初期化子が定数式のみを含む場合。
3) コンストラクタ呼び出しによって初期化されない静的またはスレッドローカルなオブジェクト (クラス型でなくても構いません) で、そのオブジェクトが値初期化される場合、またはその初期化子内のすべての式が定数式の場合。

定数初期化の効果は対応する初期化の効果と同じですが、静的またはスレッドローカルなオブジェクトのあらゆる他の初期化が始まるよりも前に完了することが保証されます。

[編集] ノート

コンパイラは、標準の初期化順序に従った場合と同じ値になることが保証できる場合は、他の静的およびスレッドローカルなオブジェクトを定数初期化を用いて初期化することが許されています。

実際には、定数初期化はコンパイル時に行われ、事前計算されたオブジェクト表現がプログラムイメージの一部として (例えば .data セクションに) 格納されます。 変数が const であり定数初期化される場合、そのオブジェクト表現はプログラムイメージの読み込み専用セクション (例えば .rodata セクション) に格納されることがあります。

[編集]

#include <iostream>
#include <array>
 
struct S {
    static const int c;
};
const int d = 10 * S::c; // 定数式ではありません。 S::c にはまだ初期化子がありません。
                         // この初期化は const の後に行われます。
const int S::c = 5; // 定数初期化。 最初に行われることが保証されます。
int main()
{
    std::cout << "d = " << d << '\n';
    std::array<int, S::c> a1; // OK、 S::c は定数式です。
//  std::array<int, d> a2;    // エラー、 d は定数式ではありません。
}

出力:

d = 50

[編集] 欠陥報告

以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。

DR 適用先 発行時の動作 正しい動作
CWG 2026 C++14 zero-init was specified to always occur first, even before constant-init no zero-init if constant init applies

[編集] 関連項目