 st8b7c:f320:99b9:690f:4595:cd17:293a:c069get (st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant)    Defined in header <variant>         (1)  (since C++17)     template< st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t I, class... Types >
constexpr st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant_alternative_t<I, st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>>& 
    get( st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>& v );
      template< st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t I, class... Types >
constexpr st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant_alternative_t<I, st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>>&& 
    get( st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>&& v );
      template< st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t I, class... Types >
constexpr const st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant_alternative_t<I, st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>>& 
    get( const st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>& v );
      template< st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t I, class... Types >
constexpr const st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant_alternative_t<I, st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>>&&
    get( const st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>&& v );
      (2)  (since C++17)     template< class T, class... Types >
constexpr T& get( st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>& v );
      template< class T, class... Types >
constexpr T&& get( st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>&& v );
      template< class T, class... Types >
constexpr const T& get( const st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>& v );
      template< class T, class... Types >
constexpr const T&& get( const st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<Types...>&& v );
     
1) Index-based value accessor: If v.index() == I, returns a reference to the value stored in v. Otherwise, throws st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_variant_access. The call is ill-formed if I is not a valid index in the variant. 
2) Type-based value accessor: If v holds the alternative T, returns a reference to the value stored in v. Otherwise, throws st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_variant_access. The call is ill-formed if T is not a unique element of Types....  Template parameters    I   -   index to look up 
   T   -   unique type to look up 
   Types...   -   types forming the variant 

  Parameters    v   -   a variant 

  Return value Reference to the value stored in the variant.
 Exceptions 
1,2) Throws st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_variant_access on errors.  Example  #include <variant>
#include <string>
#include <iostream>
 
int main()
{
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069variant<int, float> v{12}, w;
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << st8b7c:f320:99b9:690f:4595:cd17:293a:c069get<int>(v) << '\n';
    w = st8b7c:f320:99b9:690f:4595:cd17:293a:c069get<int>(v);
    w = st8b7c:f320:99b9:690f:4595:cd17:293a:c069get<0>(v); // same effect as the previous line
 
//  st8b7c:f320:99b9:690f:4595:cd17:293a:c069get<double>(v); // error: no double in [int, float]
//  st8b7c:f320:99b9:690f:4595:cd17:293a:c069get<3>(v);      // error: valid index values are 0 and 1
 
    try
    {
        w = 42.0f;
        st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << st8b7c:f320:99b9:690f:4595:cd17:293a:c069get<float>(w) << '\n'; // ok, prints 42
        w = 42;
        st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << st8b7c:f320:99b9:690f:4595:cd17:293a:c069get<float>(w) << '\n'; // throws
    }
    catch (st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_variant_access const& ex)
    {
        st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << ex.what() << ": w contained int, not float\n";
    }
} Possible output:
12
42
Unexpected index: w contained int, not float   See also     get_if
(C++17)   obtains a pointer to the value of a pointed-to variant given the index or the type (if unique), returns null on error  (function template) 
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069get(st8b7c:f320:99b9:690f:4595:cd17:293a:c069tuple)
(C++11)   tuple accesses specified element  (function template) 
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069get(st8b7c:f320:99b9:690f:4595:cd17:293a:c069array)
(C++11)   accesses an element of an array  (function template) 
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069get(st8b7c:f320:99b9:690f:4595:cd17:293a:c069pair)
(C++11)   accesses an element of a pair  (function template) 
    get(st8b7c:f320:99b9:690f:4595:cd17:293a:c069ranges8b7c:f320:99b9:690f:4595:cd17:293a:c069subrange)
(C++20)   obtains iterator or sentinel from a st8b7c:f320:99b9:690f:4595:cd17:293a:c069ranges8b7c:f320:99b9:690f:4595:cd17:293a:c069subrange  (function template) 
  

    
{'path': 'public/docs/cpp/utility/variant/get.html', 'source': 'DevDocs.IO'}