Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::variant<Types...>::index

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      constexprstd::size_t index()constnoexcept;
      (since C++17)

      Returns the zero-based index of the alternative that is currently held by the variant.

      If the variant isvalueless_by_exception, returnsvariant_npos.

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <variant> int main(){std::variant<int,std::string> v="abc";std::cout<<"v.index = "<< v.index()<<'\n';    v={};std::cout<<"v.index = "<< v.index()<<'\n';}

      Output:

      v.index = 1v.index = 0

      [edit]See also

      checks if avariant currently holds a given type
      (function template)[edit]
      reads the value of the variant given the index or the type (if the type is unique), throws on error
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/index&oldid=172838"

      [8]ページ先頭

      ©2009-2025 Movatter.jp