std::get(std::variant)
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
std::variant
(C++26)
get(std::variant)
Defined in header
<variant>
(1)
(since C++17)
template< std::size_t I, class... Types >
constexpr std::variant_alternative_t <I, std::variant <Types...>>&
template< std::size_t I, class... Types >
constexpr std::variant_alternative_t <I, std::variant <Types...>>&&
template< std::size_t I, class... Types >
constexpr const std::variant_alternative_t <I, std::variant <Types...>>&
template< std::size_t I, class... Types >
constexpr const std::variant_alternative_t <I, std::variant <Types...>>&&
(2)
(since C++17)
template< class T, class... Types >
constexpr T& get( std::variant <Types...>& v );
constexpr T& get( std::variant <Types...>& v );
template< class T, class... Types >
constexpr T&& get( std::variant <Types...>&& v );
constexpr T&& get( std::variant <Types...>&& v );
template< class T, class... Types >
constexpr const T& get( const std::variant <Types...>& v );
constexpr const T& get( const std::variant <Types...>& v );
template< class T, class... Types >
constexpr const T&& get( const std::variant <Types...>&& v );
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....[edit] Template parameters
I
-
index to look up
T
-
unique type to look up
Types...
-
types forming the
variant
[edit] Parameters
v
-
a
variant
[edit] Return value
Reference to the value stored in the variant.
[edit] Exceptions
1,2) Throws std::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_access const& ex) { std::cout << ex.what() << ": w contained int, not float\n"; } }
Possible output:
12 42 Unexpected index: w contained int, not float
[edit] See also
(C++17)
variant
given the index or the type (if unique), returns null on error (function template) [edit]
(C++26)
(function template) [edit]