std::get(std::variant) (3) - Linux Manuals
std::get(std::variant): std::get(std::variant)
NAME
std::get(std::variant) - std::get(std::variant)
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 std::variant_alternative_t<
I, std::variant<Types...>
> const& get(const std::variant<Types...>& v); (1) (since C++17)
template <std::size_t I, class... Types>
constexpr std::variant_alternative_t<
I, std::variant<Types...>
> const&& 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); (2) (since C++17)
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);
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....
Parameters
I - index to look up
Type - unique type to look up
v - a variant
Return value
Reference to the value stored in the variant.
Exceptions
1,2) Throws std::bad_variant_access on errors.
Example
// Run this code
See also
get_if obtains a pointer to the value of a pointed-to variant given the index or the type (if unique), returns null on error
(C++17)
std::get(std::tuple) (function template)
std::get(std::array) (function template)
std::get(std::pair) accesses an element of a pair
(C++11)