constant initialization
De cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Définit les valeurs initiales des constantes statiques
Original:
Sets the initial values of the static constants
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Sommaire |
[modifier] Syntaxe
static T & ref = constexpr;
|
(1) | ||||||||
static T object = constexpr;
|
(2) | ||||||||
[modifier] Explication
Initialisation constante est effectuée après l'initialisation à zéro des objets statiques et local des threads et avant tout autre initialisation. Seules les variables suivantes sont constants initialisé:
Original:
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:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
Références statiques ou locale de thread, s'il est lié à l'électricité statique ou à une lvalue temporaire. et si chaque expression (y compris les conversions implicites) dans l'initialisation de la référence est un expression constante .
Original:
Static or thread-local references, if it is bound to static lvalue or to a temporary. and if every expression (including implicit conversions) in l'initialisation of the reference is a expression constante.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
Objet statique ou locale au thread de type de classe qui est initialisé par un appel de constructeur, si le constructeur est constexpr et tous les arguments du constructeur (y compris les conversions implicites) sont des expressions constantes, et si les initialiseurs de liste d'initialisation du constructeur et le contreventement ou- initialiseurs égalité des memebers classe ne contiennent que des expressions constantes .
Original:
Static or thread-local object of class type that is initialized by a constructor call, if the constructor is 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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
Objet statique ou local des threads (pas nécessairement de type classe), qui n'est pas initialisé par un appel de constructeur, si chaque expression dans un initialiseur est une expression constante .
Original:
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 text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Les effets de l'initialisation constante sont les mêmes que les effets de l'initialisation correspondant, sauf qu'il est garanti qu'elle soit complète avant toute autre initialisation d'un objet statique ou locale au thread commence, et elle peut être effectuée au moment de la compilation .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
L'objet qui a été initialisé par l'initialisation constante peut être utilisé dans des expressions constantes, par exemple dans une déclaration de tableau .
Original:
The object that was initialized by constant initialization can be used in constant expressions, e.g. in an array declaration.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier] Notes
Le compilateur est autorisé à initialiser d'autres objets statiques et local des threads à l'aide d'initialisation constante, si l'on peut garantir que la valeur serait le même que si l'ordre standard d'initialisation a été suivie. De tels objets peuvent être utilisés dans des expressions constantes, mais cette utilisation n'est pas portable .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier] Exemple
#include <iostream> #include <array> struct S { static const int c; }; const int d = 10 * S::c; // not a constant expression: S::c has no preceding // initializer, this initialization happens after const const int S::c = 5; // constant initialization, guaranteed to happen first int main() { std::cout << "d = " << d << '\n'; std::array<int, S::c> a1; // OK: S::c is a constant expression // std::array<int, d> a2; // error: d is not a constant expression }
Résultat :
d = 50