Namespaces
Variants

std:: variant_npos

From cppreference.net
Utilities library
헤더 파일에 정의됨 <variant>
inline constexpr std:: size_t variant_npos = - 1 ;
(C++17부터)

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

#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)