std::is_within_lifetime
cppreference.net에서 발췌
template< class T >
consteval bool is_within_lifetime( const T* ptr ) noexcept;
|
(C++26부터)에 정의됨 | |
포인터 ptr가 수명 내에 있는 객체를 가리키는지 확인합니다.
핵심 상수 표현식으로 E의 평가 동안, std::is_within_lifetime에 대한 호출은 ptr가 다음을 가리키지 않는 한 잘못된 형식입니다.
- 상수 표현식에서 사용 가능한
- 객체이거나,
E내에서 완전한 객체의 수명이 시작된 객체입니다.
매개변수
| p | - | 탐지할 포인터 |
반환값
true 만약 포인터 ptr 가 수명 주기 내에 있는 객체를 가리키는 경우; 그렇지 않으면 false .
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_is_within_lifetime
|
202306L
|
(C++26) | union 대안이 활성 상태인지 확인 |
예제
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) |
타입이 암시적 수명 유형인지 확인합니다 (클래스 템플릿) |