Difference between revisions of "cpp/utility/variant/get"
(+) |
m (seealso other gets) |
||
Line 74: | Line 74: | ||
{{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/container/array/dsc get}} | ||
+ | {{dsc inc | cpp/utility/pair/dsc get}} | ||
{{dsc end}} | {{dsc end}} |
Revision as of 08:54, 12 July 2016
Defined in header <variant>
|
||
template <std::size_t I, class... Types> constexpr std::variant_alternative_t< |
(1) | (since C++17) |
template <class T, class... Types> constexpr T& get(std::variant<Types...>& v); |
(2) | (since C++17) |
v
. Otherwise, throws std::bad_variant_access. The call is ill-formed if I
is not a valid index in the variant or if T_I
is a (possibly cv-qualified) type void
.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...
or if T_I
is a (possibly cv-qualified) type void
.Contents |
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
Example
#include <variant> #include <string> int main() { std::variant<int, float> v{12}, w; int i = std::get<int>(v); 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 { std::get<float>(w); // w contains int, not float: will throw } catch (std::bad_variant_access&) {} }
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) |