Constant expressions

Defines an expression that can be evaluated at compile time.

# Notes

Feature-test macro Value Std Feature __cpp_constexpr_in_decltype 201711L (C++20)(DR11) Generation of function and variable definitions when needed for constant evaluation __cpp_constexpr_dynamic_alloc 201907L (C++20) Operations for dynamic storage duration in constexpr functions __cpp_constexpr 202306L (C++26) constexpr cast from void*: towards constexpr type-erasure 202406L (C++26) constexpr placement new and new[] __cpp_constexpr_exceptions 202411L (C++26) constexpr exceptions

# Example

This section is incompleteReason: no example

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 94C++98arithmetic constant expressions could notinvolve variables and static data membersthey can
CWG 366C++98expressions involving string literalscould be integral constant expressionsthey are not
CWG 457C++98expressions involving volatile variablescould be integral constant expressionsthey are not
CWG 1293C++11it was unclear whether string literalsare usable in constant expressionsthey 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 from void* could achieve the same effectprohibited conversionsfrom type cv void* toa pointer-to-object type
CWG 1313C++11undefined behavior was permitted;all pointer subtraction was prohibitedUB prohibited; same-arraypointer subtraction OK
CWG 1405C++11for objects that are usable in constant expressions,their mutable subobjects were also usablethey are not usable
CWG 1454C++11passing constants through constexprfunctions via references was not allowedallowed
CWG 1455C++11converted constant expressions could only be prvaluescan be lvalues
CWG 1456C++11an address constant expression could notdesignate the address one past the end of an arrayallowed
CWG 1535C++11a typeid expression whose operand is of apolymorphic class type was not a core constantexpression even if no runtime check is involvedthe operand constraintis limited to glvalues ofpolymorphic class types
CWG 1581C++11functions needed for constant evaluation werenot required to be defined or instantiatedrequired
CWG 1613C++11core constant expressions could evaluate anyODR-used reference inside lambda expressionssome references couldnot be evaluated
CWG 1694C++11binding the value of a temporary to a static storageduration reference was a constant expressionit is not aconstant expression
CWG 1872C++11core constant expressions could invoke constexpr function templateinsantiations that do not satisfy the constexpr function requirementssuch instantiationscannot be invoked
CWG 1952C++11standard library undefined behaviorswere required to be diagnosedunspecified whetherthey are diagnosed
CWG 2022C++98the determination of constant expression mightdepend on whether copy elision is performedassume that copy elisionis always performed
CWG 2126C++11constant initialized lifetime-extended temporaries of const-qualified literal types were not usable in constant expressionsusable
CWG 2129C++11integer literals were not constant expressionsthey are
CWG 2167C++11non-member references local to an evaluationmade the evaluation non-constexprnon-memberreferences allowed
CWG 2278C++98the resolution of CWG issue 2022 was not implementableassume that copy elisionis never performed
CWG 2299C++14it was unclear whether macros in can be used in constant evaluationva_arg forbidden,va_start unspecified
CWG 2400C++11invoking a constexpr virtual function on an object not usablein constant expressions and whose lifetime began outside theexpression containing the invocation could be a constant expressionit is not aconstant expression
CWG 2490C++20(pseudo) destructor calls lackedrestrictions in constant evaluationrestriction added
CWG 2552C++23when evaluating a core constant expression, the controlflow could not pass through a declaration of a non-block variableit 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 requiredto be detected during constant evaluationrequired
CWG 2851C++11converted constant expressions didnot allow floating-point conversionsallow non-narrowingfloating-point conversions
CWG 2907C++11core constant expressions could not applylvalue-to-rvalue conversions to std::nullptr_t glvaluescan apply suchconversions
CWG 2909C++20a variable without an initializer could only beconstant-initialized if its default-initializationresults in some initialization being performedcan only be constant-initialized if its type isconst-default-initializable
CWG 2924C++11C++23it was unspecified whether an expression violatingthe constraints of [[noreturn]] (C++11) or[[assume]] (C++23) is a core constant expressionit isimplementation-defined
P2280R4C++11evaluating an expression containing an identifier expressionor *this that refers to an object or reference whose lifetimebegan outside this evaluation is not a constant expressionit can be aconstant expression

# See also