[Example 1: constexprvoid f(unsignedchar*p, int n){if(std::is_constant_evaluated()){// should not be a constexpr if statementfor(int k =0; k<n; ++k) p[k]=0;
}else{
memset(p, 0, n); // not a core constant expression}} — end example]
Remarks: During the evaluation of an expression E as a core constant expression,
a call to this function is ill-formed
unless p points to an object that is usable
in constant expressions or
whose complete object's lifetime began within E.
[Example 2: struct OptBool {union{bool b; char c; };
// note: this assumes common implementation properties for bool and char:// * sizeof(bool) == sizeof(char), and// * the value representations for true and false are distinct// from the value representation for 2constexpr OptBool(): c(2){}constexpr OptBool(bool b): b(b){}constexprauto has_value()const->bool{ifconsteval{return std::is_within_lifetime(&b); // during constant evaluation, cannot read from c}else{return c !=2; // during runtime, must read from c}}constexprautooperator*()const->constbool&{return b;
}};
constexpr OptBool disengaged;
constexpr OptBool engaged(true);
static_assert(!disengaged.has_value());
static_assert(engaged.has_value());
static_assert(*engaged);
— end example]