std::get(std::variant)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

okay so bestie, this is the page for std::get(std::variant) β€” manual page std::get(std::variant)(3) β€” from the C++ Standard Libary manual (yes, that's the header spelling, not me πŸ˜­πŸ’–). It's literally the β€œhow do I get the thing out of my variant” function, and it's giving very much C++17 main-character energy. ✨


🌸 Synopsis 🌸

Defined in header <variant>

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

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

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

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

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 >
constexpr const T& get( const std::variant<Types...>& v );

template< class T, class... Types >
constexpr const T&& get( const std::variant<Types...>&& v );

The index-based overloads are (1), the type-based overloads are (2), and both are (since C++17). 🫢

okay so there are two whole flavors of this iconic little accessor, and the numbered deets are:

  1. Index-based value accessor: If v.index() == I, returns a reference to the value stored in v. Otherwise, throws std::bad_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 std::bad_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. That's the whole parameter. slay. πŸ’…

✨ Return value ✨

Reference to the value stored in the variant. no cap. πŸ’«


πŸ”₯ Exceptions πŸ”₯

  • 1,2) β€” throws std::bad_variant_access on errors. bestie, it will absolutely let you know when the vibe check fails. πŸ’€

🌈 Example 🌈

// Run this code

#include <variant>
#include <string>
#include <iostream>

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_access const& ex)
    {
        std::cout << 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)
  • get(std::tuple) (C++11) β€” tuple accesses specified element (function template)
  • get(std::array) (C++11) β€” accesses an element of an array (function template)
  • get(std::pair) (C++11) β€” accesses an element of a pair (function template)
  • get(std::ranges::subrange) (C++20) β€” obtains iterator or sentinel from a std::ranges::subrange (function template)

πŸ‘‘ Category πŸ‘‘

  • * Uses of dcl rev begin with nonempty note

mwah πŸ’‹ this lil' page was served fresh from http://cppreference.com, version/date 2024.06.10, and it's manual page std::get(std::variant)(3). byeee bestie πŸ«ΆπŸ’–πŸŽ€

πŸ“¦ where this page comes from