The constinit specifier declares a variable with static or thread storage duration. If a variable is declared with constinit , its initializing declaration must be applied with constinit. If a variable declared with constinit has dynamic initialization, the program is ill-formed. If no constinit declaration is reachable at the point of the initializing declaration, the program is ill-formed, no diagnostic required.
When the declared variable is a reference, constinit is equivalent to constexpr. When the declared variable is an object, constexpr mandates that the object must have static initialization and constant destruction and makes the object const-qualified, however, constinit does not mandate constant destruction and const-qualification.
As a result, an object of a type which has constexpr constructors and no constexpr destructor e. Create account Log in. Namespaces Page Discussion. Views View Edit History.
From cppreference. Keywords Escape sequences. Namespace declaration. Namespace aliases. Fundamental types Enumeration types Function types. Compound types Union types. Default initialization Value initialization Zero initialization Copy initialization Direct initialization.
Expressions Value categories Order of evaluation. Operators Operator precedence. Class declaration Constructors this pointer. They exist even if no objects of the class have been defined.
Static data members cannot be mutable. Static data members of a class in namespace scope have external linkage if the class itself has external linkage is not a member of unnamed namespace. Local classes classes defined inside functions and unnamed classes, including member classes of unnamed classes, cannot have static data members. A static data member may be declared inline. An inline static data member can be defined in the class definition and may specify an initializer.
It does not need an out-of-class definition:. If a static data member of integral or enumeration type is declared const and not volatile , it can be initialized with an initializer in which every expression is a constant expression , right inside the class definition:. If a static data member of LiteralType is declared constexpr , it must be initialized with an initializer in which every expression is a constant expression, right inside the class definition:. If a static data member is declared constexpr , it is implicitly inline and does not need to be redeclared at namespace scope.
This redeclaration without an initializer formerly required as shown above is still permitted, but is deprecated. Create account Log in. Namespaces Page Discussion. Views View Edit History. From cppreference. Keywords Escape sequences. Namespace declaration. Namespace aliases. Fundamental types Enumeration types Function types. Compound types Union types. Default initialization Value initialization Zero initialization Copy initialization Direct initialization.
Expressions Value categories Order of evaluation. Operators Operator precedence. Class declaration Constructors this pointer. Access specifiers friend specifier.
Class template Function template. Inline assembly. Compiler support. Freestanding and hosted. Language support library. Collectives on Stack Overflow. Learn more.
Static const member initialization in templated class Ask Question. Asked 11 years, 5 months ago. Active 7 years, 6 months ago. Viewed 15k times. Improve this question. Gratian Lup Gratian Lup 1, 3 3 gold badges 19 19 silver badges 27 27 bronze badges. You are missing the 'const' modifier in the definition. Are you sure this is your code? You shouldn't get any errors. Ropez: The const only matters in the declaration.
Interestingly VS does not have this problem. Add a comment. Active Oldest Votes. Improve this answer.
0コメント