A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../algorithm/../utility/variant/variant_npos.html below:

std::variant_npos - cppreference.com

(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.

#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
[edit] See also returns the zero-based index of the alternative held by the variant
(public member function) [edit] checks if the variant is in the invalid state
(public member function) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4