std:: get_if (std::variant)
|
定义于头文件
<variant>
|
||
| (1) | (C++17 起) | |
|
template
<
std::
size_t
I,
class
...
Types
>
constexpr
std::
add_pointer_t
<
std::
variant_alternative_t
<
I,
std::
variant
<
Types...
>>>
|
||
|
template
<
std::
size_t
I,
class
...
Types
>
constexpr
std::
add_pointer_t
<
const
std::
variant_alternative_t
<
I,
std::
variant
<
Types...
>>>
|
||
| (2) | (C++17 起) | |
|
template
<
class
T,
class
...
Types
>
constexpr
std::
add_pointer_t
<
T
>
|
||
|
template
<
class
T,
class
...
Types
>
constexpr
std::
add_pointer_t
<
const
T
>
|
||
I
不是变体中的有效索引,则调用格式错误。
I
表示
T
在
Types...
中从零开始的索引。如果
T
不是
Types...
中的唯一元素,则调用格式错误。
目录 |
模板参数
| I | - | 要查找的索引 |
| Type | - | 要查找的唯一类型 |
参数
| pv | - | 指向变体的指针 |
返回值
指向所指向变体中存储的值的指针,若出错则为空指针。
示例
#include <iostream> #include <variant> int main() { auto check_value = [](const std::variant<int, float>& v) { if (const int* pval = std::get_if<int>(&v)) std::cout << "variant value: " << *pval << '\n'; else std::cout << "failed to get value!" << '\n'; }; std::variant<int, float> v{12}, w{3.f}; check_value(v); check_value(w); }
输出:
variant value: 12 failed to get value!
参阅
|
(C++17)
|
根据索引或类型(若类型唯一)读取 variant 的值,错误时抛出异常
(函数模板) |