Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::is_within_lifetime

      From cppreference.com
      <cpp‎ |types
       
       
      Utilities library
       
      Defined in header<type_traits>
      template<class T>
      constevalbool is_within_lifetime(const T* ptr)noexcept;
      (since C++26)

      Determines whether the pointerptr points to an object that is within itslifetime.

      During the evaluation of an expressionE as a core constant expression, a call tostd::is_within_lifetime is ill-formed unlessptr points to an object

      Contents

      [edit]Parameters

      p - pointer to detect

      [edit]Return value

      true if pointerptr points to an object that is within its lifetime; otherwisefalse.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_is_within_lifetime202306L(C++26)Checking if a union alternative is active

      [edit]Example

      std::is_within_lifetime can be used to check whether a union member is active:

      Run this code
      #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 2constexpr optional_bool(): c(2){}constexpr optional_bool(bool b): b(b){} constexprauto 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}} constexprauto 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);}
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/is_within_lifetime&oldid=173648"

      [8]ページ先頭

      ©2009-2025 Movatter.jp