std::get
STD::获取%28 std::变体%29
Defined in header | | |
---|---|---|
| (1) | (since C++17) |
template <std::size_t I, class... Types> constexpr std::variant_alternative_t< I, std::variant<Types...> >& get(std::variant<Types...>& v | | |
template <std::size_t I, class... Types> constexpr std::variant_alternative_t< I, std::variant<Types...> >&& get(std::variant<Types...>&& v | | |
template <std::size_t I, class... Types> constexpr std::variant_alternative_t< I, std::variant<Types...> > const& get(const std::variant<Types...>& v | | |
template <std::size_t I, class... Types> constexpr std::variant_alternative_t< I, std::variant<Types...> > const&& get(const std::variant<Types...>&& v | | |
| (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%29基于索引的值访问器:如果v.index() == I
中存储的值的引用。v
.否则,抛出std::bad_variant_access
...打电话是不正确的,如果I
在变体中不是有效的索引。
基于2%29类型的值访问器:如果v
持有另一种选择T
中存储的值的引用。v
.否则,抛出std::bad_variant_access
...打电话是不正确的,如果T
的唯一元素Types...
...
参数
I | - | index to look up |
---|---|---|
Type | - | unique type to look up |
v | - | a variant |
返回值
引用存储在变体中的值。
例外
1,2%29投std::bad_variant_access
关于错误。
例
二次
#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&) {}
}
二次
另见
get_if (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) |
---|---|
std::get(std::tuple) | tuple accesses specified element (function template) |
std::get(std::array) | accesses an element of an array (function template) |
std::get(std::pair) (C++11) | accesses an element of a pair (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。