最終更新日時(UTC):
が更新

履歴 編集

function
<type_traits>

std::is_within_lifetime(C++26)

namespace std {
  template<class T>
  consteval bool is_within_lifetime(const T* p) noexcept; // (1) C++26
}

概要

共用体の指定されたメンバがアクティブかを定数式で判定する。

定数式では非アクティブな共用体メンバへのアクセスができないため、以下のような非アクティブなメンバの値を使用してアクティブメンバを判定する方法が使用できない。

struct OptBool {
  union { bool b; char c; };

  OptBool() : c(2) { }
  OptBool(bool b) : b(b) { }

  auto has_value() const -> bool {
    return c != 2;
  }

  auto operator*() -> bool& {
    return b;
  }
};

この関数を使用することで、コンパイル時に指定メンバがアクティブかを判定することができる。

戻り値

pが有効期間内にあるオブジェクトへのポインタであればtrue、そうでなければfalseを返す。

備考

  • Eを定数式として評価する際、pが定数式で使用可能なオブジェクトを指しているか、そのオブジェクトの完全な有効期間がE内で始まっていない限り、この関数の呼び出しは不適格となる

#include <type_traits>

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);   // 定数式評価中は、cを読み取ることはできない
    } else {
      return c != 2;                        // 実行時評価中は、cを読み取らないといけない
    }
  }

  constexpr auto operator*() -> bool& {
    return b;
  }
};

int main() {
  constexpr OptBool disengaged;
  constexpr OptBool engaged(true);
  static_assert(!disengaged.has_value());
  static_assert(engaged.has_value());
  static_assert(*engaged);
}

出力

バージョン

言語

  • C++26

処理系

参照