std::variant_npos(3)
π std::variant_npos(3) β C++ Standard Libary π
okay bestie, welcome to the tiny-but-mighty page for std::variant_npos from the C++ Standard Libary β yes, that spelling is exactly what the header says π
πΈ NAME πΈ
std::variant_nposβstd::variant_npos - std::variant_nposβ she's self-titled, no notes, periodt. π
β¨ Synopsis β¨
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. That's the whole vibe check. π¦
π§ // 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
π See also π
indexβ returns the zero-based index of the alternative held by the variant (public member function).valueless_by_exceptionβ checks if the variant is in the invalid state (public member function).
kiss kiss, bestie β this page came from http://cppreference.com, dated 2024.06.10, and it's literally std::variant_npos(3) π
β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms