Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      constexpr specifier(since C23)

      From cppreference.com
      <c‎ |language
       
       
       
       

      A scalar object declared with theconstexpr storage-class specifier is aconstant. It must be fully and explicitly initialized according to the static initialization rules. It still has linkage appropriate to its declaration and it exists at runtime to have its address taken; it simply cannot be modified at runtime in any way, i.e., the compiler can use its knowledge of the object’s fixed value in any otherconstant expression.

      Additionally, the constant expression that is used for the initializer of such a constant is checked at compile time.

      An initializer of floating-point type must be evaluated with the translation-time floating-point environment.

      There are some restrictions on the type of an object that can be declared withconstexpr. Namely, the following constructs are not allowed to beconstexpr:

      Contents

      [edit]Keywords

      constexpr

      [edit]Example

      Run this code
      #include <fenv.h>#include <stdio.h> int main(void){    constexprfloat f=23.0f;    constexprfloat g=33.0f;fesetround(FE_TOWARDZERO);    constexprfloat h= f/ g;// is not affected by fesetround() aboveprintf("%f\n", h);}

      Output:

      0.696969

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.7.2 Storage-class specifiers (p: 98-103)

      [edit]See also

      C++ documentation forconstexpr type specifier
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/constexpr&oldid=183173"

      [8]ページ先頭

      ©2009-2025 Movatter.jp