Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::variant_npos

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      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.

      Run this code
      #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

      [edit]See also

      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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/variant_npos&oldid=172846"

      [8]ページ先頭

      ©2009-2026 Movatter.jp