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
vholds 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 thevariant
[edit] Parameters [edit] Return value
Reference to the value stored in the variant.
[edit] Exceptions [edit] Example#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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4