Difference between revisions of "cpp/utility/variant/get"
From cppreference.com
m (improved example) |
m (fmt.) |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/title|get {{ | + | {{cpp/title|get{{petty|(std::variant)}}}} |
{{cpp/utility/variant/navbar}} | {{cpp/utility/variant/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | variant}} | + | {{dcl header|variant}} |
− | {{dcl rev begin | since=c++17 | num=1 }} | + | {{dcl rev begin|since=c++17|num=1}} |
{{dcl| | {{dcl| | ||
template< std::size_t I, class... Types > | template< std::size_t I, class... Types > | ||
− | constexpr std::variant_alternative_t<I, std::variant<Types...>>& | + | constexpr std::variant_alternative_t<I, std::variant<Types...>>& |
− | get( std::variant<Types...>& v ); | + | get( std::variant<Types...>& v ); |
}} | }} | ||
{{dcl| | {{dcl| | ||
template< std::size_t I, class... Types > | template< std::size_t I, class... Types > | ||
− | constexpr std::variant_alternative_t<I, std::variant<Types...>>&& | + | constexpr std::variant_alternative_t<I, std::variant<Types...>>&& |
− | get( std::variant<Types...>&& v ); | + | get( std::variant<Types...>&& v ); |
}} | }} | ||
{{dcl| | {{dcl| | ||
template< std::size_t I, class... Types > | template< std::size_t I, class... Types > | ||
− | constexpr const std::variant_alternative_t<I, std::variant<Types...>>& | + | constexpr const std::variant_alternative_t<I, std::variant<Types...>>& |
− | get( const std::variant<Types...>& v ); | + | get( const std::variant<Types...>& v ); |
}} | }} | ||
{{dcl| | {{dcl| | ||
Line 25: | Line 25: | ||
}} | }} | ||
{{dcl rev end}} | {{dcl rev end}} | ||
− | {{dcl rev begin | since=c++17 | num=2 }} | + | {{dcl rev begin|since=c++17|num=2}} |
{{dcl| | {{dcl| | ||
template< class T, class... Types > | template< class T, class... Types > | ||
− | constexpr T& get( std::variant<Types...>& v ); | + | constexpr T& get( std::variant<Types...>& v ); |
}} | }} | ||
{{dcl| | {{dcl| | ||
template< class T, class... Types > | template< class T, class... Types > | ||
− | constexpr T&& get( std::variant<Types...>&& v ); | + | constexpr T&& get( std::variant<Types...>&& v ); |
}} | }} | ||
{{dcl| | {{dcl| | ||
template< class T, class... Types > | template< class T, class... Types > | ||
− | constexpr const T& get( const std::variant<Types...>& v ); | + | constexpr const T& get( const std::variant<Types...>& v ); |
}} | }} | ||
{{dcl| | {{dcl| | ||
template< class T, class... Types > | template< class T, class... Types > | ||
− | constexpr const T&& get( const std::variant<Types...>&& v ); | + | constexpr const T&& get( const std::variant<Types...>&& v ); |
}} | }} | ||
{{dcl rev end}} | {{dcl rev end}} | ||
{{dcl end}} | {{dcl end}} | ||
− | @1@ Index-based value accessor: If {{c|v.index() | + | @1@ Index-based value accessor: If {{c|1=v.index() == I}}, returns a reference to the value stored in {{c|v}}. Otherwise, throws {{lc|std::bad_variant_access}}. The call is ill-formed if {{tt|I}} is not a valid index in the variant. |
− | @2@ Type-based value accessor: If {{c|v}} holds the alternative {{tt|T}}, returns a reference to the value stored in {{ | + | @2@ Type-based value accessor: If {{c|v}} holds the alternative {{tt|T}}, returns a reference to the value stored in {{c|v}}. Otherwise, throws {{lc|std::bad_variant_access}}. The call is ill-formed if {{tt|T}} is not a unique element of {{c|Types...}}. |
===Template parameters=== | ===Template parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | I | index to look up }} | + | {{par|I|index to look up}} |
− | {{par | T | unique type to look up }} | + | {{par|T|unique type to look up}} |
− | {{par | Types... | types forming the {{tt|variant}} }} | + | {{par|Types...|types forming the {{tt|variant}}}} |
{{par end}} | {{par end}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | v | a {{tt|variant}} }} | + | {{par|v|a {{tt|variant}}}} |
{{par end}} | {{par end}} | ||
Line 67: | Line 67: | ||
===Example=== | ===Example=== | ||
− | {{example|code= | + | {{example |
− | + | |code= | |
− | + | ||
#include <iostream> | #include <iostream> | ||
+ | #include <string> | ||
+ | #include <variant> | ||
int main() | int main() | ||
Line 82: | Line 83: | ||
// std::get<3>(v); // error: valid index values are 0 and 1 | // std::get<3>(v); // error: valid index values are 0 and 1 | ||
− | try | + | try |
{ | { | ||
− | std::cout << std::get<float>(w) << '\n'; | + | 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&) | + | catch (std::bad_variant_access const& ex) |
{ | { | ||
− | std::cout << "w contained int, not float\n"; | + | std::cout << ex.what() << ": w contained int, not float\n"; |
} | } | ||
} | } | ||
− | |output=12 | + | |p=true |
− | w contained int, not float | + | |output= |
+ | 12 | ||
+ | 42 | ||
+ | Unexpected index: w contained int, not float | ||
}} | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/utility/variant/dsc get_if}} | + | {{dsc inc|cpp/utility/variant/dsc get_if}} |
− | {{dsc inc | cpp/utility/tuple/dsc get}} | + | {{dsc inc|cpp/utility/tuple/dsc get}} |
− | {{dsc inc | cpp/container/array/dsc get}} | + | {{dsc inc|cpp/container/array/dsc get}} |
− | {{dsc inc | cpp/utility/pair/dsc get}} | + | {{dsc inc|cpp/utility/pair/dsc get}} |
+ | {{dsc inc|cpp/ranges/subrange/dsc get}} | ||
+ | {{dsc inc|cpp/numeric/complex/dsc get}} | ||
{{dsc end}} | {{dsc end}} | ||
− | {{langlinks|ja|zh}} | + | {{langlinks|es|ja|ru|zh}} |
Latest revision as of 08:24, 22 August 2024
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 ); |
||
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 ); |
||
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....Contents |
[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) |
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) |
(C++11) |
tuple accesses specified element (function template) |
(C++11) |
accesses an element of an array (function template) |
(C++11) |
accesses an element of a pair (function template) |
(C++20) |
obtains iterator or sentinel from a std::ranges::subrange (function template) |
(C++26) |
obtains a reference to real or imaginary part from a std::complex (function template) |