Defined in header <variant> | ||
---|---|---|
(1) | (since C++17) | |
template <std::size_t I, class... Types> constexpr std::add_pointer_t< std::variant_alternative_t<I, std::variant<Types...>> > get_if(std::variant<Types...>* pv) noexcept; | ||
template <std::size_t I, class... Types> constexpr std::add_pointer_t< const std::variant_alternative_t<I, variant<Types...>> > get_if(const std::variant<Types...>* pv) noexcept; | ||
(2) | (since C++17) | |
template <class T, class... Types> constexpr std::add_pointer_t<T> get_if(std::variant<Types...>* pv) noexcept; | ||
template <class T, class... Types> constexpr std::add_pointer_t<const T> get_if(const std::variant<Types...>* pv) noexcept; |
pv
is not a null pointer and pv->index() == I
, returns a pointer to the value stored in the variant pointed to by pv
. Otherwise, returns a null pointer value. The call is ill-formed if I
is not a valid index in the variant.I
being the zero-based index of T
in Types...
. The call is ill-formed if T
is not a unique element of Types...
.I | - | index to look up |
Type | - | unique type to look up |
pv | - | pointer to a variant |
Pointer to the value stored in the pointed-to variant or null pointer on error.
#include <variant> #include <iostream> int main() { std::variant<int, float> v{12}; if(auto pval = std::get_if<int>(&v)) std::cout << "variant value: " << *pval << '\n'; else std::cout << "failed to get value!" << '\n'; }
Output:
variant value: 12
(C++17) | reads the value of the variant given the index or the type (if the type is unique), throws on error (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/variant/get_if