|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <variant> | ||
inlineconstexprstd::size_t variant_npos=-1; | (since C++17) | |
This is a special value equal to the largest value representable by the typestd::size_t, used as the return value ofindex() whenvalueless_by_exception() istrue.
#include <iostream>#include <stdexcept>#include <string>#include <variant> struct Demon{ Demon(int){} Demon(const Demon&){throwstd::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(conststd::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: falseException: copy ctorindex == npos: truevalueless: true
returns the zero-based index of the alternative held by thevariant(public member function)[edit] | |
checks if thevariant is in the invalid state(public member function)[edit] |