constant initialization
![]() | Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Sets the initial values of the static constants
Indice |
[modifica]Sintassi
static T& ref= constexpr; | (1) | ||||||||
static Tobject= constexpr; | (2) | ||||||||
[modifica]Spiegazione
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 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 object that was initialized by constant initialization can be used in constant expressions, e.g. in an array declaration.
[modifica]Note
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.
[modifica]Esempio
#include <iostream>#include <array> struct S {staticconstint c;};constint d =10* S::c;// not a constant expression: S::c has no preceding// initializer, this initialization happens after constconstint S::c=5;// constant initialization, guaranteed to happen firstint 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}
Output:
d = 50