std::variant_npos
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
std::variant
(C++26)
variant_npos
Defined in header
<variant>
inline constexpr std::size_t variant_npos = -1;
(since C++17)
This is a special value equal to the largest value representable by the type std::size_t , used as the return value of index() when valueless_by_exception() is true.
Run this code
#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'; } }
Possible output:
index == npos: false Exception: copy ctor index == npos: true valueless: true