[meta.const.eval] (original) (raw)

21 Metaprogramming library [meta]

21.3 Metaprogramming and type traits [type.traits]

21.3.11 Constant evaluation context [meta.const.eval]

constexpr bool is_constant_evaluated() noexcept;

Effects: Equivalent to:if consteval { return true;} else { return false;}

[Example 1: constexpr void f(unsigned char *p, int n) { if (std::is_constant_evaluated()) { for (int k = 0; k<n; ++k) p[k] = 0;} else { memset(p, 0, n); } } — _end example_]

consteval bool is_within_lifetime(const auto* p) noexcept;

Returns: true if p is a pointer to an object that is within its lifetime ([basic.life]); otherwise, false.

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; };constexpr OptBool() : c(2) { } constexpr OptBool(bool b) : b(b) { } constexpr auto has_value() const -> bool { if consteval { return std::is_within_lifetime(&b); } else { return c != 2; } } constexpr auto operator*() const -> const bool& { return b;} };constexpr OptBool disengaged;constexpr OptBool engaged(true);static_assert(!disengaged.has_value());static_assert(engaged.has_value());static_assert(*engaged); — _end example_]