Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::get(std::variant)

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      Defined in header<variant>
      (1)(since C++17)
      template<std::size_t I,class...Types>

      constexprstd::variant_alternative_t<I,std::variant<Types...>>&

          get(std::variant<Types...>& v);
      template<std::size_t I,class...Types>

      constexprstd::variant_alternative_t<I,std::variant<Types...>>&&

          get(std::variant<Types...>&& v);
      template<std::size_t I,class...Types>

      constexprconststd::variant_alternative_t<I,std::variant<Types...>>&

          get(conststd::variant<Types...>& v);
      template<std::size_t I,class...Types>

      constexprconststd::variant_alternative_t<I,std::variant<Types...>>&&

          get(conststd::variant<Types...>&& v);
      (2)(since C++17)
      template<class T,class...Types>
      constexpr T& get(std::variant<Types...>& v);
      template<class T,class...Types>
      constexpr T&& get(std::variant<Types...>&& v);
      template<class T,class...Types>
      constexprconst T& get(conststd::variant<Types...>& v);
      template<class T,class...Types>
      constexprconst T&& get(conststd::variant<Types...>&& v);
      1) Index-based value accessor: Ifv.index()== I, returns a reference to the value stored inv. Otherwise, throwsstd::bad_variant_access. The call is ill-formed ifI is not a valid index in the variant.
      2) Type-based value accessor: Ifv holds the alternativeT, returns a reference to the value stored inv. Otherwise, throwsstd::bad_variant_access. The call is ill-formed ifT is not a unique element ofTypes....

      Contents

      [edit]Template parameters

      I - index to look up
      T - unique type to look up
      Types... - types forming thevariant

      [edit]Parameters

      v - avariant

      [edit]Return value

      Reference to the value stored in the variant.

      [edit]Exceptions

      1,2) Throwsstd::bad_variant_access on errors.

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <variant> int main(){std::variant<int,float> v{12}, w;std::cout<< std::get<int>(v)<<'\n';    w= std::get<int>(v);    w= std::get<0>(v);// same effect as the previous line //  std::get<double>(v); // error: no double in [int, float]//  std::get<3>(v);      // error: valid index values are 0 and 1 try{        w=42.0f;std::cout<< std::get<float>(w)<<'\n';// ok, prints 42        w=42;std::cout<< std::get<float>(w)<<'\n';// throws}catch(std::bad_variant_accessconst& ex){std::cout<< ex.what()<<": w contained int, not float\n";}}

      Possible output:

      1242Unexpected index: w contained int, not float

      [edit]See also

      (C++17)
      obtains a pointer to the value of a pointed-tovariant given the index or the type (if unique), returns null on error
      (function template)[edit]
      tuple accesses specified element
      (function template)[edit]
      accesses an element of anarray
      (function template)[edit]
      accesses an element of apair
      (function template)[edit]
      obtains iterator or sentinel from astd::ranges::subrange
      (function template)[edit]
      obtains a reference to real or imaginary part from astd::complex
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/get&oldid=175380"

      [8]ページ先頭

      ©2009-2025 Movatter.jp