Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Constant expressions

      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
       
      Expressions
      General
      Literals
      Operators
      Conversions
       

      Defines anexpression that can be evaluated at compile time.

      Such expressions can be used as constant template arguments, array sizes, and in other contexts that require constant expressions, e.g.

      int n=1;std::array<int, n> a1;// Error: “n” is not a constant expressionconstint cn=2;std::array<int, cn> a2;// OK: “cn” is a constant expression

      Contents

      [edit]Definition

      An expression that belongs to any of the constant expression categories listed below is aconstant expression.

      C++98 constant expression categories

      Integral constant expression (C++98)

      In the following places, C++ requires expressions that evaluate to an integral or enumeration constant:

      An expression satisfying all following conditions is anintegral constant-expression :

      • It only involves the following entities:
      • literals of arithmetic types
      • enumerators
      • variables or static data members satisfying all following conditions:
      • They are const-qualified.
      • They are not volatile-qualified.
      • They are of integral or enumeration types.
      • They are initialized with constant expressions.
      • It does not use any floating-point literals, unless they areexplicitly converted to integral or enumeration types.
      • It does not apply any conversion to non-integral and non-enumeration types.
      • It does not use any of the following entities except in the operands ofsizeof:
      • function
      • class object
      • pointer
      • reference
      • assignment operator
      • increment operator
      • decrement operator
      • function-call operator
      • comma operator

      Other constant expression categories

      Other expressions are considered constant expressions only for the purpose ofconstant initialization. Such a constant expression must be one of the following expressions:

      • an expression that evaluates to anull pointer value
      • an expression that evaluates to a null pointer-to-member value
      • an arithmetic constant expression
      • an address constant expression
      • a reference constant expression
      • an address constant expression for a complete object type, plus or minus an integral constant expression
      • a pointer-to-member constant expression

      Anarithmetic constant expression is an expression satisfying the requirements for an integral constant expression, with the following exceptions:

      • Floating-point literals can be used without explicit conversion.
      • Conversions to floating-point types can be applied.

      Anaddress constant expression is an expression of pointer type satisfying all following conditions:

      • explicitly using the address-of operator
      • implicitly using a constant template parameter of pointer type
      • using an expression of array or function type
      • The expression does not call any function.
      • The expression uses explicit pointer conversions (exceptdynamic_cast) and the following operators without accessing the result object:
      • subscript operator
      • indirection operator
      • address-of operator
      • member access operator
      • If the subscript operator is used, one of its operands is an integral constant expression.

      Areference constant expression is an expression of reference type satisfying all following conditions:

      • The reference designates an object of static storage duration, a constant template parameter of reference type, or a function. The reference does not designate a member or base class of non-POD class type.
      • The expression does not call any function.
      • The expression uses explicit reference conversions (exceptdynamic_cast) and the following operators without accessing the result object:
      • subscript operator
      • indirection operator
      • address-of operator
      • member access operator
      • If the subscript operator is used, one of its operands is an integral constant expression.

      Apointer-to-member constant expression is an expression of pointer-to-member type where the pointer is created by applying the address-of operator to a qualified identifier, optionally preceded by an explicit pointer-to-member conversion.

      (until C++11)

      The following expressions are collectively calledconstant expressions :

      • the address of an object with static storage duration
      • the address of a function
      • anull pointer value
      (since C++11)
      (until C++14)

      The following entities arepermitted results of a constant expression :

      • temporary objects withstatic storage duration
      • non-temporary objects with static storage duration whose values satisfy the constraints listed below
      • non-immediate(since C++20)functions

      Aconstant expression is either a glvaluecore constant expression that refers to an entity that is a permitted result of a constant expression, or a prvalue core constant expression whose value satisfies the following constraints:

      • If the value is an object of class type, each non-static data member of reference type refers to an entity that is a permitted result of a constant expression.
      • If the value is an object ofscalar type, it does not have anindeterminate value.
      • If the value is ofpointer type, it is one of the following values:
      • the address of an object with static storage duration
      • the address past the end of an object with static storage duration
      • the address of a non-immediate(since C++20) function
      • anull pointer value
      • If the value is of pointer-to-member-function type, it does not designate an immediate function.
      (since C++20)
      • If the value is an object of class or array type, each subobject satisfies these constraints for the value.
      (since C++14)
      (until C++26)

      Aconstant expression is either a glvaluecore constant expression that refers to an object or a non-immediate function, or a prvalue core constant expression whose value satisfies the following constraints:

      (since C++26)

      When determining whether an expression is a constant expression,copy elision is assumed not to be performed.

      The C++98 definition of constant expressions is entirely within the collapse box. The following description applies to C++11 and later C++ versions.

      [edit]Literal type

      The following types are collectively calledliteral types :

      • It has atrivial destructor(until C++20)constexpr destructor(since C++20).
      • All of its non-static non-variant data members and base classes are of non-volatile literal types.
      • It is one of the following types:
      (since C++17)
      • anaggregate union type that satisfies one of the following conditions:
      • It has novariant member.
      • It has at least one variant member of non-volatile literal type.
      • a non-union aggregate type, and each of itsanonymous union members satisfies one of the following conditions:
      • It has no variant member.
      • It has at least one variant member of non-volatile literal type.
      • a type with at least oneconstexpr constructor (template) that is not a copy or move constructor

      Only objects of literal types can be created within a constant expression.

      [edit]Core constant expression

      Acore constant expression is any expression whose evaluationwould not evaluate any one of the following language constructs:

      Language construct    Version    Paper(s)
      thethis pointer, except in aconstexpr function that is being evaluated as part of the expression, or when appearing in an implicit or explicit class member access expressionN2235
      a control flow that passes through a declaration of ablock variable with static or threadstorage duration that is notusable in constant expressions(since C++23)P2242R3
      This section is incomplete
      Reason: Transfer the contents from the raw-HTML ordered list below to the wikitable above, while adding the papers/CWG issues that introduced the corresponding item to the standard. The mini-examples are not preserved, they can be combined to form a large example at the bottom of this page.
      1. a function call expression that calls a function (or a constructor) that is not declaredconstexpr
        constexprint n=std::numeric_limits<int>::max();// OK: max() is constexprconstexprint m=std::time(nullptr);// Error: std::time() is not constexpr
      2. a function call to a constexpr function which is declared, but not defined
      3. a function call to a constexpr function/constructor template instantiation where the instantiation fails to satisfyconstexpr function/constructor requirements.
      4. a function call to a constexpr virtual function, invoked on an object whose dynamic type is constexpr-unknown
      5. an expression that would exceed the implementation-defined limits
      6. an expression whose evaluation leads to any form of core languageundefined or erroneous(since C++26) behavior, except for any potential undefined behavior introduced bystandard attributes.
        constexprdouble d1=2.0/1.0;// OKconstexprdouble d2=2.0/0.0;// Error: not definedconstexprint n=std::numeric_limits<int>::max()+1;// Error: overflowint x, y, z[30];constexprauto e1=&y-&x;// Error: undefinedconstexprauto e2=&z[20]-&z[3];// OKconstexprstd::bitset<2> a;constexprbool b= a[2];// UB, but unspecified if detected
      7. (until C++17) alambda expression
      8. an lvalue-to-rvalueimplicit conversion unless applied to...
        1. a glvalue of type (possibly cv-qualified)std::nullptr_t
        2. a non-volatile literal-type glvalue that designates an object that isusable in constant expressions
          int main(){conststd::size_t tabsize=50;int tab[tabsize];// OK: tabsize is a constant expression// because tabsize is usable in constant expressions// because it has const-qualified integral type, and// its initializer is a constant initializer std::size_t n=50;conststd::size_t sz= n;int tab2[sz];// Error: sz is not a constant expression// because sz is not usable in constant expressions// because its initializer was not a constant initializer}
        3. a non-volatile literal-type glvalue that refers to a non-volatile object whose lifetime began within the evaluation of this expression
      9. an lvalue-to-rvalueimplicit conversion or modification applied to a non-active member of aunion or its subobject (even if it shares a common initial sequence with the active member)
      10. an lvalue-to-rvalue implicit conversion on an objectwhose value is indeterminate
      11. an invocation of implicit copy/move constructor/assignment for a union whose active member is mutable (if any), with lifetime beginning outside the evaluation of this expression
      12. (until C++20) an assignment expression that would change the active member of a union
      13. conversion frompointer tovoid to a pointer-to-object typeT* unless the pointer holds a null pointer value or points to an object whose type issimilar toT(since C++26)
      14. dynamic_castwhose operand is a glvalue that refers to an object whose dynamic type is constexpr-unknown(since C++20)
      15. reinterpret_cast
      16. (until C++20) pseudo-destructor call
      17. (until C++14) an increment or a decrement operator
      18. (since C++14) modification of an object, unless the object has non-volatile literal type and its lifetime began within the evaluation of the expression
        constexprint incr(int& n){return++n;} constexprint g(int k){constexprint x= incr(k);// Error: incr(k) is not a core constant// expression because lifetime of k// began outside the expression incr(k)return x;} constexprint h(int k){int x= incr(k);// OK: x is not required to be initialized// with a core constant expressionreturn x;} constexprint y= h(1);// OK: initializes y with the value 2// h(1) is a core constant expression because// the lifetime of k begins inside the expression h(1)
      19. (since C++20) a destructor call or pseudo destructor call for an object whose lifetime did not begin within the evaluation of this expression
      20. atypeid expression applied to a glvalue of polymorphic typeand that glvalue refers to an object whose dynamic type is constexpr-unknown(since C++20)
      21. anew expression, unless one of the following conditions is satisfied:(since C++20)
        • The selectedallocation function is a replaceable global allocation function and the allocated storage is deallocated within the evaluation of this expression.
        (since C++20)
        • The selected allocation function is a non-allocating form with an allocated typeT, and the placement argument satisfies all following conditions:
        • It points to:
        • an object whose type is similar toT, ifT is not an array type, or
        • the first element of an object of a type similar toT, ifT is an array type.
        • It points to storage whose duration began within the evaluation of this expression.
        (since C++26)
      22. adelete expression, unless it deallocates a region of storage allocated within the evaluation of this expression(since C++20)
      23. (since C++20) Coroutines: anawait-expression or ayield-expression
      24. (since C++20) athree-way comparison when the result is unspecified
      25. an equality or relational operator whose result is unspecified
      26. (until C++14) an assignment or a compound assignment operator
      27. (until C++26) a throw expression
      28. (since C++26) a construction of an exception object, unless the exception object and all of its implicit copies created by invocations ofstd::current_exception orstd::rethrow_exception are destroyed within the evaluation of this expression
        constexprvoid check(int i){if(i<0)throw i;} constexprbool is_ok(int i){try{        check(i);}catch(...){returnfalse;}returntrue;} constexprbool always_throw(){throw12;returntrue;} static_assert(is_ok(5));// OKstatic_assert(!is_ok(-1));// OK since C++26static_assert(always_throw());// Error: uncaught exception
      29. anasm-declaration
      30. an invocation of theva_arg macro
      31. agoto statement
      32. adynamic_cast ortypeid expressionornew expression(since C++26) that would throw an exceptionwhere no definition of the exception type is reachable(since C++26)
      33. inside a lambda expression, a reference tothis or to a variable defined outside that lambda, if that reference would be an odr-use
        void g(){constint n=0; constexprint j=*&n;// OK: outside of a lambda-expression [=]{constexprint i= n;// OK: 'n' is not odr-used and not captured here.constexprint j=*&n;// Ill-formed: '&n' would be an odr-use of 'n'.};}

        note that if the odr-use takes place in a function call to a closure, it does not refer tothis or to an enclosing variable, since it accesses a closure's data member instead

        // OK: 'v' & 'm' are odr-used but do not occur in a constant-expression// within the nested lambdaauto monad=[](auto v){return[=]{return v;};};auto bind=[](auto m){return[=](auto fvm){return fvm(m());};}; // OK to have captures to automatic objects created during constant expression evaluation.static_assert(bind(monad(2))(monad)()== monad(2)());
        (since C++17)

      [edit]Extra requirements

      Even if an expressionE does not evaluate anything stated above, it is implementation-defined whetherE is a core constant expression if evaluatingE would result inruntime-undefined behavior.

      Even if an expressionE does not evaluate anything stated above, it is unspecified whetherE is a core constant expression if evaluatingE would evaluate any of the following:

      For the purposes of determining whether an expression is a core constant expression, the evaluation of the body of a member function ofstd::allocator<T> is ignored ifT is a literal type.

      For the purposes of determining whether an expression is a core constant expression, the evaluation of a call to a trivial copy/move constructor or copy/move assignment operator of aunion is considered to copy/move the active member of the union, if any.

      For the purposes of determining whether an expression is a core constant expression, the evaluation of an identifier expression that names astructured bindingbd has the following semantics:

      • Ifbd is an lvalue referring to the object bound to an invented referenceref, the behavior is as ifref were nominated.
      • Otherwise, ifbd names an array element, the behavior is that of evaluatinge[i], wheree is the name of the variable initialized from the initializer of the structured binding declaration, andi is the index of the element referred to bybd.
      • Otherwise, ifbd names a class member, the behavior is that of evaluatinge.m, wheree is the name of the variable initialized from the initializer of the structured binding declaration, andm is the name of the member referred to bybd.
      (since C++26)

      During the evaluation of the expression as a core constant expression, all identifier expressions and uses of*this that refer to an object or reference whose lifetime began outside the evaluation of the expression are treated as referring to a specific instance of that object of that object or reference whose lifetime and that of all subobjects (including all union members) includes the entire constant evaluation.

      • For such an object that is notusable in constant expressions(since C++20), the dynamic type of the object isconstexpr-unknown.
      • For such a reference that is not usable constant expression(since C++20)s, the reference is treated as binding to an unspecified object of the referenced type whose lifetime and that of all subobjects includes the entire constant evaluation and whose dynamic type is constexpr-unknown.

      [edit]Integral constant expression

      Integral constant expression is an expression of integral or unscoped enumeration type implicitly converted to a prvalue, where the converted expression is a core constant expression.

      If an expression of class type is used where an integral constant expression is expected, the expression iscontextually implicitly converted to an integral or unscoped enumeration type.

      [edit]Converted constant expression

      Aconverted constant expression of typeT is an expressionimplicitly converted to typeT, where the converted expression is a constant expression, and the implicit conversion sequence contains only:

      (since C++17)

      And if anyreference binding takes place, it can only bedirect binding.

      The following contexts require a converted constant expression:

      (since C++14)
      (since C++26)

      Acontextually converted constant expression of typebool is an expression,contextually converted tobool, where the converted expression is a constant expression and the conversion sequence contains only the conversions above.

      The following contexts require a contextually converted constant expression of typebool:

      (until C++23)
      (since C++17)
      (until C++23)
      (since C++20)


      Constituent entities

      Theconstituent values of an objectobj are defined as follows:

      • Ifobj has scalar type, the constituent value is the value ofobj.
      • Otherwise, the constituent values are the constituent values of any directsubobjects ofobj other thaninactive union members.

      Theconstituent references of an objectobj include the following references:

      • any direct members ofobj that have reference type
      • the constituent references of any direct subobjects ofobj other than inactive union members

      Theconstituent values andconstituent references of a variablevar are defined as follows:

      • Ifvar declares an object, the constituent values and references are the constituent values and references of that object.
      • Ifvar declares a reference, the constituent reference is that reference.

      For any constituent referenceref of a variablevar, ifref is bound to a temporary object or subobject thereof whose lifetime is extended to that ofref, the constituent values and references of that temporary object are also constituent values and references ofvar, recursively.

      Constexpr-representable entities

      Objects with static storage duration isconstexpr-referenceable at any point in the program.

      An objectobj with automatic storage duration isconstexpr-referenceable from a pointP if the smallestscope enclosing the variablevar and the smallest scope enclosingP that are the same function parameter scope that does not associate with the parameter list of arequires expression, wherevar is the variable corresponding toobj’s complete object or the variable to whose lifetime that ofobj isextended.

      An object or referencex isconstexpr-representable at a pointP if all following conditions are satisfied:

      • For each constituent value ofx that points to an objectobj,obj is constexpr-referenceable fromP.
      • For each constituent value ofx that points past an objectobj,obj is constexpr-referenceable fromP.
      • For each constituent reference ofx that refers to an objectobj,obj is constexpr-referenceable fromP.
      (since C++26)

      Constant-initialized entities

      A variable or temporary objectobj isconstant-initialized if all following conditions are satisfied:

      • Either it has an initializer, or its type isconst-default-constructible.
      • Thefull-expression of its initialization is a constant expression in the context of requiring a constant expression, except that ifobj is an object, that full-expression may also invokeconstexpr constructors forobj and its subobjects even if those objects are of non-literal class types.
      (until C++26)

      A variablevar isconstant-initializable if all following conditions are satisfied:

      • Thefull-expression of its initialization is a constant expression in the context of requiring a constant expression, where allcontract assertions use the “ignore” evaluation semantic.
      • Immediately after the initializing declaration ofvar, the object or reference declared byvar is constexpr-representable.
      • If the object or referencex declared byvar has static or thread storage duration,x is constexpr-representable at the nearest point whose immediate scope is a namespace scope that follows the initializing declaration ofvar.

      A constant-initializable variable isconstant-initialized if either it has an initializer, or its type isconst-default-constructible.

      (since C++26)

      Usable in constant expressions

      A variable ispotentially-constant if it is aconstexpr variable or it has reference or non-volatile const-qualified integral or enumeration type.

      A constant-initialized potentially-constant variablevar isusable in constant expressions at a pointP ifvar’s initializing declarationD is reachable fromP and any of the following conditions is satisfied:

      • var is aconstexpr variable.
      • var is not initialized to aTU-local value.
      • P is in the same translation unit asD.

      An object or reference isusable in constant expressions at a pointP if it is one of the following entities:

      • a variable that is usable in constant expressions atP
      • a temporary object of non-volatile const-qualified literal type whose lifetime is extended to that of a variable that is usable in constant expressions atP
      • atemplate parameter object
      • astring literal object
      • a non-mutable subobject of any of the above
      • a reference member of any of the above
      (until C++26)

      An object or reference ispotentially usable in constant expressions at a pointP if it is one of the following entities:

      • a variable that is usable in constant expressions atP
      • a temporary object of non-volatile const-qualified literal type whose lifetime is extended to that of a variable that is usable in constant expressions atP
      • atemplate parameter object
      • astring literal object
      • a non-mutable subobject of any of the above
      • a reference member of any of the above

      An object or reference isusable in constant expressions at pointP if it is an object or reference that is potentially usable in constant expressions atP and is constexpr-representable atP.

      (since C++26)

      Manifestly constant-evaluated expressions

      The following expressions (including conversions to the destination type) aremanifestly constant-evaluated :

      Whether an evaluation occurs in a manifestly constant-evaluated context can be detected bystd::is_constant_evaluated andif consteval(since C++23).

      (since C++20)

      [edit]Functions and variables needed for constant evaluation

      Following expressions or conversions arepotentially constant evaluated:

      A function isneeded for constant evaluation if it is a constexpr function andnamed by an expression that is potentially constant evaluated.

      A variable isneeded for constant evaluation if it is either a constexpr variable or is of non-volatile const-qualified integral type or of reference type and theidentifier expression that denotes it is potentially constant evaluated.

      Definition of a defaulted function and instantiation of afunction template specializationorvariable template specialization(since C++14) are triggered if the functionor variable(since C++14) is needed for constant evaluation.

      [edit]Constant subexpression

      Aconstant subexpression is an expression whose evaluation assubexpression of an expressione would not prevente from being acore constant expression, wheree is not any of the following expressions:

      (since C++20)

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_constexpr_in_decltype201711L(C++20)
      (DR11)
      Generation of function and variable definitions whenneeded for constant evaluation
      __cpp_constexpr_dynamic_alloc201907L(C++20)Operations for dynamic storage duration inconstexpr functions
      __cpp_constexpr202306L(C++26)constexpr cast fromvoid*: towards constexpr type-erasure
      202406L(C++26)constexpr placementnew andnew[]
      __cpp_constexpr_exceptions202411L(C++26)constexpr exceptions:[1],[2]

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 94C++98arithmetic constant expressions could not
      involve variables and static data members
      they can
      CWG 366C++98expressions involving string literals
      could be integral constant expressions
      they are not
      CWG 457C++98expressions involving volatile variables
      could be integral constant expressions
      they are not
      CWG 1293C++11it was unclear whether string literals
      are usable in constant expressions
      they are usable
      CWG 1311C++11volatile glvalues could be used in constant expressionsprohibited
      CWG 1312C++11reinterpret_cast is prohibited in constant expressions,
      but casting to and fromvoid* could achieve the same effect
      prohibited conversions
      from typecvvoid* to
      a pointer-to-object type
      CWG 1313C++11undefined behavior was permitted;
      all pointer subtraction was prohibited
      UB prohibited; same-array
      pointer subtraction OK
      CWG 1405C++11for objects that are usable in constant expressions,
      their mutable subobjects were also usable
      they are not usable
      CWG 1454C++11passing constants through constexpr
      functions via references was not allowed
      allowed
      CWG 1455C++11converted constant expressions could only be prvaluescan be lvalues
      CWG 1456C++11an address constant expression could not
      designate the address one past the end of an array
      allowed
      CWG 1535C++11atypeid expression whose operand is of a
      polymorphic class type was not a core constant
      expression even if no runtime check is involved
      the operand constraint
      is limited to glvalues of
      polymorphic class types
      CWG 1581C++11functions needed for constant evaluation were
      not required to be defined or instantiated
      required
      CWG 1613C++11core constant expressions could evaluate any
      odr-used reference inside lambda expressions
      some references could
      not be evaluated
      CWG 1694C++11binding the value of a temporary to a static storage
      duration reference was a constant expression
      it is not a
      constant expression
      CWG 1872C++11core constant expressions could invokeconstexpr function template
      instantiations that do not satisfy theconstexpr function requirements
      such instantiations
      cannot be invoked
      CWG 1952C++11standard library undefined behaviors
      were required to be diagnosed
      unspecified whether
      they are diagnosed
      CWG 2022C++98the determination of constant expression might
      depend on whether copy elision is performed
      assume that copy elision
      is always performed
      CWG 2126C++11constant initialized lifetime-extended temporaries of const-
      qualified literal types were not usable in constant expressions
      usable
      CWG 2129C++11integer literals were not constant expressionsthey are
      CWG 2167C++11non-member references local to an evaluation
      made the evaluation non-constexpr
      non-member
      references allowed
      CWG 2278C++98the resolution ofCWG issue 2022 was not implementableassume that copy elision
      is never performed
      CWG 2299C++14it was unclear whether macros in<cstdarg>
      can be used in constant evaluation
      va_arg forbidden,
      va_start unspecified
      CWG 2400C++11invoking a constexpr virtual function on an object not usable
      in constant expressions and whose lifetime began outside the
      expression containing the invocation could be a constant expression
      it is not a
      constant expression
      CWG 2490C++20(pseudo) destructor calls lacked
      restrictions in constant evaluation
      restriction added
      CWG 2552C++23when evaluating a core constant expression, the control
      flow could not pass through a declaration of a non-block variable
      it can
      CWG 2558C++11an indeterminate value could be a constant expressionnot a constant expression
      CWG 2647C++20variables of volatile-qualified types could be potentially-constantthey are not
      CWG 2763C++11the violation of[[noreturn]] was not required
      to be detected during constant evaluation
      required
      CWG 2851C++11converted constant expressions did
      not allow floating-point conversions
      allow non-narrowing
      floating-point conversions
      CWG 2907C++11core constant expressions could not apply
      lvalue-to-rvalue conversions tostd::nullptr_t glvalues
      can apply such
      conversions
      CWG 2909C++20a variable without an initializer could only be
      constant-initialized if its default-initialization
      results in some initialization being performed
      can only be constant-
      initialized if its type is
      const-default-initializable
      CWG 2924C++11
      C++23
      it was unspecified whether an expression violating
      the constraints of[[noreturn]] (C++11) or
      [[assume]] (C++23) is a core constant expression
      it is
      implementation-defined
      P2280R4C++11evaluating an expression containing an identifier expression
      or*this that refers to an object or reference whose lifetime
      began outside this evaluation is not a constant expression
      it can be a
      constant expression

      [edit]See also

      constexpr specifier(C++11) specifies that the value of a variable or function can be computed at compile time[edit]
      (C++11)(deprecated in C++17)(removed in C++20)
      checks if a type is a literal type
      (class template)[edit]
      C documentation forConstant expressions
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/constant_expression&oldid=183020"

      [8]ページ先頭

      ©2009-2025 Movatter.jp