GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
* Sign Up! *

Support
Customer Portal
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::is_within_lifetime(3) C++ Standard Libary std::is_within_lifetime(3)

std::is_within_lifetime - std::is_within_lifetime


Defined in header <type_traits>
template< class T > (since C++26)
consteval bool is_within_lifetime( const T* ptr ) noexcept;


Determines whether the pointer ptr points to an object that is within its lifetime.


During the evaluation of an expression E as a core constant expression, a call to
std::is_within_lifetime is ill-formed unless ptr points to an object


* that is usable in constant expressions, or
* whose complete object’s lifetime began within E.


p - pointer to detect


true if pointer ptr points to an object that is within its lifetime; otherwise
false.


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 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);
}

2024.06.10 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.