Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Constant expressions

      From cppreference.com
      <c‎ |language
       
       
       
       

      Several varieties of expressions are known asconstant expressions.

      Contents

      [edit]Preprocessor constant expression

      The expression following#if or#elif must expand to

      Character constants, when evaluated in#if-expressions, may be interpreted in the sourcecharacter set, the execution character set, or some other implementation-defined character set.

      Integer arithmetic in#if-expressions is performed using the semantics ofintmax_t for signed types anduintmax_t for unsigned types.

      (since C99)

      [edit]Integer constant expression

      An integer constant expression is an expression that consists only of

      • _Alignof(until C23)alignas(since C23) operators
      (since C11)
      • named and compound literal constants that are of integer type or that are of arithmetic type and are the immediate operands of casts
      (since C23)

      Integer constant expressions are evaluated at compile time. The following contexts require expressions that are known asinteger constant expressions:

      (since C99)
      (since C11)
      • The number of bits N of a bit-precise integer type (_BitInt(N))
      (since C23)

      [edit]Static initializer

      Expressions that are used in theinitializers of objects with static and thread_localstorage durationor declared with theconstexpr storage-class specifier(since C23) must be either string literals or expressions that may be one of the following

      1)arithmetic constant expression, which is an expression of any arithmetic type that consists of
      (since C11)
      • named or compound literal constants of arithmetic type
      (since C23)
      2) a null pointer constant (e.g.,NULL).
      3)address constant expression, which is
      • a null pointer
      • lvalue designating an object of staticstorage duration or a function designator, converted to a pointer either
      • by using the unary address-of operator
      • by casting an integer constant to a pointer
      • by array-to-pointer or function-to-pointer implicitconversion.
      4)address constant expression of some complete object type, plus or minus aninteger constant expression.
      5) anamed constant which is, an identifier that is
      • an enumeration constant
      • a predefined constant (one oftrue,false ornullptr)
      • declared with storage-class specifierconstexpr and has an object type
      or a postfix expression that applies the. member access operator to a named constant of structure or union type, even recursively.
      6) acompound literal constant, which is
      • acompound literal with storage-class specifierconstexpr
      • a postfix expression that applies the. member access operator to a compound literal constant of structure or union type, even recursively.

      Astructure or union constant is a named constant or compound literal constant with structure or union type, respectively. If the member-access operator. accesses a member of a union constant, the accessed member shall be the same as the member that is initialized by the union constant’s initializer.

      (since C23)
      7) constant expression of one of the other forms accepted by the implementation.

      Unlike with integer constant expressions, static initializer expressions are not required to be evaluated at compile time; the compiler is at liberty to turn such initializers into executable code which is invoked prior to program startup.

      staticint i=2||1/0;// initializes i to value 1
      This section is incomplete
      Reason: other mini-examples

      The value of a floating-point static initializer is never less accurate than the value of the same expression executed at run time, but it may be better.

      [edit]Floating-point constant expressions

      Arithmetic constant expressions of floating-point types that are not used in static initializers are always evaluated as-if during run-time and are affected by thecurrent rounding (ifFENV_ACCESS is on) and report errors as specified inmath_errhandling.

      void f(void){#pragma STDC FENV_ACCESS ONstaticfloat x=0.0/0.0;// static initializer: does not raise an exceptionfloat w[]={0.0/0.0};// raises an exceptionfloat y=0.0/0.0;// raises an exceptiondouble z=0.0/0.0;// raises an exception}

      [edit]Notes

      If an expression evaluates to a value that is not representable by its type, it cannot be used as a constant expression.

      Implementations may accept other forms of constant expressions. However, these constant expressions are not considered as integer constant expressions, arithmetic constant expressions, or address constant expressions, and thus cannot be used in the contexts requiring these kinds of constant expressions. For example,int arr[(int)+1.0]; declares a VLA.

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.6 Constant expressions (p: 95-96)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.6 Constant expressions (p: 76-77)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.6 Constant expressions (p: 106-107)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.6 Constant expressions (p: 95-96)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.4 CONSTANT EXPRESSIONS

      [edit]See also

      C++ documentation forConstant expressions
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/constant_expression&oldid=183172"

      [8]ページ先頭

      ©2009-2025 Movatter.jp