Namespaces
Variants

std::variant_npos

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)



 
 
헤더 <variant>
inline constexpr std::size_t variant_npos = -1;
(C++17부터)에 정의됨

이는 std::size_t 타입으로 표현 가능한 가장 큰 값과 같은 특별한 값으로, valueless_by_exception()이 일 때true index()의 반환값으로 사용됩니다.

#include <iostream>
#include <stdexcept>
#include <string>
#include <variant>

struct Demon
{
    Demon(int) {}
    Demon(const Demon&) { throw std::domain_error("copy ctor"); }
    Demon& operator= (const Demon&) = default;
};

int main()
{
    std::variant<int, Demon> var{42};
    std::cout
        << std::boolalpha
        << "index == npos: " << (var.index() == std::variant_npos) << '\n';

    try { var = Demon{666}; } catch (const std::domain_error& ex)
    {
        std::cout
            << "Exception: " << ex.what() << '\n'
            << "index == npos: " << (var.index() == std::variant_npos) << '\n'
            << "valueless: " << var.valueless_by_exception() << '\n';
    }
}

가능한 출력:

index == npos: false
Exception: copy ctor
index == npos: true
valueless: true

참고 항목

variant 가 보유한 대안의 0 기반 인덱스를 반환합니다
(public member function)
variant 가 무효 상태인지 확인합니다
(public member function)