Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Constant initialization

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
       

      Sets the initial values of thestatic variables to a compile-time constant.

      Contents

      [edit]Explanation

      Constant initialization is performed in the following cases:

      • Initializing an object ofPOD type with static storage duration with a constant expression.
      (until C++11)
      • Initializing a reference withstatic orthread storage duration, where all following conditions are satisfied:
      • an lvalue designating an object with static storage duration
      • a temporary object
      • asubobject of a temporary object
      • a function
      • Initializing an object with static or thread storage duration, and one of the following conditions is satisfied:
      • If the object is initialized by a constructor call, where the initialization full-expression is a constant expression, except that it may also invokeconstexpr constructors for the object and its subobjects (even if those objects are of non-literal class types).
      • Otherwise, either the object isvalue-initialized or every full-expression that appears in its initializer is a constant expression.
      (since C++11)
      (until C++17)
      (since C++17)
      (until C++20)
      (since C++20)

      The effects of constant initialization are the same as the effects of the corresponding initialization, except that it is guaranteed that it is complete before any other initialization of a static or thread-local(since C++11) object begins.

      [edit]Notes

      The compiler is permitted to initialize other static and thread-local(since C++11) objects using constant initialization, if it can guarantee that the value would be the same as if the standard order of initialization was followed.

      Constant initialization usually happens when the program loads into memory, as part of initializing the program's runtime environment.

      [edit]Example

      Run this code
      #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 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}

      Output:

      d = 50

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 441C++98references could not be constant initializedmade constant initializable
      CWG 1489C++11it was unclear whether value-initializing
      an object can be a constant initialization
      it can
      CWG 1747C++11binding a reference to a function could not be constant initializationit can
      CWG 1834C++11binding a reference to an xvalue could not be constant initializationit can

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/constant_initialization&oldid=180229"

      [8]ページ先頭

      ©2009-2025 Movatter.jp