Namespaces
Variants

std::is_within_lifetime

cppreference.net에서 발췌
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐지됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
교체 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



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

참고 항목

타입이 암시적 수명 유형인지 확인합니다
(클래스 템플릿)