std::is_within_lifetime
ja.cppreference.net より
| ヘッダ <type_traits>
|
||
template< class T >
consteval bool is_within_lifetime( const T* ptr ) noexcept;
|
で定義 (C++26以上) | |
ポインタ ptr が、その生存期間内にあるオブジェクトを指しているかどうかを判定します。
E をコア定数式として評価する際、std::is_within_lifetime の呼び出しは、ptr が以下のいずれかのオブジェクトを指していない限り、ill-formed です。
- 定数式で使用可能なオブジェクト
- その完全オブジェクトの生存期間が
E内に開始されたオブジェクト。
パラメータ
| p | - | 検出するポインタ |
戻り値
true ポインタ ptr が生存期間内のオブジェクトを指している場合;そうでなければ false 。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_is_within_lifetime
|
202306L
|
(C++26) | 共用体の代替要素がアクティブかどうかのチェック |
例
std::is_within_lifetime
は、共用体のメンバがアクティブかどうかをチェックするために使用できます:
このコードを実行
#include <type_traits> // an optional boolean type occupying only one byte, // assuming sizeof(bool) == sizeof(char) struct optional_bool { union { bool b; char c; }; // assuming the value representations for true and false // are distinct from the value representation for 2 constexpr optional_bool() : c(2) {} constexpr optional_bool(bool b) : b(b) {} constexpr auto has_value() const -> bool { if consteval { return std::is_within_lifetime(&b); // during constant evaluation, // cannot read from c { else { return c != 2; // during runtime, must read from c { { constexpr auto operator*() -> bool& { return b; { { int main() { constexpr optional_bool disengaged; constexpr optional_bool engaged(true); static_assert(!disengaged.has_value()); static_assert(engaged.has_value()); static_assert(*engaged); {
関連項目
(C++23) |
型が暗黙の生存期間型であるかどうかを調べる (クラステンプレート) |