Skip to content

Commit 0facada

Browse files
burblebeetkoeppe
authored andcommitted
P2641R4 Checking if a union alternative is active
The feature test macro has been renamed from __cpp_lib_within_lifetime to __cpp_lib_is_within_lifetime, which seems more appropriate and follows existing practice.
1 parent 04cf6ed commit 0facada

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

source/meta.tex

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@
583583

584584
// \ref{meta.const.eval}, constant evaluation context
585585
constexpr bool is_constant_evaluated() noexcept;
586+
template<class T>
587+
consteval bool is_within_lifetime(const T*) noexcept;
586588
}
587589
\end{codeblock}
588590

@@ -2432,6 +2434,61 @@
24322434
\end{example}
24332435
\end{itemdescr}
24342436

2437+
\indexlibraryglobal{is_within_lifetime}%
2438+
\begin{itemdecl}
2439+
template<class T>
2440+
consteval bool is_within_lifetime(const T* p) noexcept;
2441+
\end{itemdecl}
2442+
2443+
\begin{itemdescr}
2444+
\pnum
2445+
\returns
2446+
\tcode{true} if \tcode{p} is a pointer to an object that is
2447+
within its lifetime\iref{basic.life}; otherwise, \tcode{false}.
2448+
2449+
\pnum
2450+
\remarks
2451+
During the evaluation of an expression \tcode{E} as a core constant expression,
2452+
a call to this function is ill-formed
2453+
unless \tcode{p} points to an object that is usable
2454+
in constant expressions or
2455+
whose complete object's lifetime began within \tcode{E}.
2456+
2457+
\pnum
2458+
\begin{example}
2459+
\begin{codeblock}
2460+
struct OptBool {
2461+
union { bool b; char c; };
2462+
2463+
// note: this assumes common implementation properties for \tcode{bool} and \tcode{char}:
2464+
// * \tcode{sizeof(bool) == sizeof(char)}, and
2465+
// * the value representations for \tcode{true} and \tcode{false} are distinct
2466+
// from the value representation for \tcode{2}
2467+
constexpr OptBool() : c(2) { }
2468+
constexpr OptBool(bool b) : b(b) { }
2469+
2470+
constexpr auto has_value() const -> bool {
2471+
if consteval {
2472+
return std::is_within_lifetime(&b); // during constant evaluation, cannot read from \tcode{c}
2473+
} else {
2474+
return c != 2; // during runtime, must read from \tcode{c}
2475+
}
2476+
}
2477+
2478+
constexpr auto operator*() -> bool& {
2479+
return b;
2480+
}
2481+
};
2482+
2483+
constexpr OptBool disengaged;
2484+
constexpr OptBool engaged(true);
2485+
static_assert(!disengaged.has_value());
2486+
static_assert(engaged.has_value());
2487+
static_assert(*engaged);
2488+
\end{codeblock}
2489+
\end{example}
2490+
\end{itemdescr}
2491+
24352492
\rSec1[ratio]{Compile-time rational arithmetic}
24362493

24372494
\rSec2[ratio.general]{In general}

source/support.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@
679679
#define @\defnlibxname{cpp_lib_is_pointer_interconvertible}@ 201907L // freestanding, also in \libheader{type_traits}
680680
#define @\defnlibxname{cpp_lib_is_scoped_enum}@ 202011L // freestanding, also in \libheader{type_traits}
681681
#define @\defnlibxname{cpp_lib_is_swappable}@ 201603L // freestanding, also in \libheader{type_traits}
682+
#define @\defnlibxname{cpp_lib_is_within_lifetime}@ 202306L // also in \libheader{type_traits}
682683
#define @\defnlibxname{cpp_lib_jthread}@ 201911L // also in \libheader{stop_token}, \libheader{thread}
683684
#define @\defnlibxname{cpp_lib_latch}@ 201907L // also in \libheader{latch}
684685
#define @\defnlibxname{cpp_lib_launder}@ 201606L // freestanding, also in \libheader{new}

0 commit comments

Comments
 (0)