首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::get_if

Defined in header <variant>

(1)

(since C++17)

template <std::size_t I, class... Types> constexpr std::add_pointer_t< std::variant_alternative_t<I, std::variant<Types...>> > get_if(std::variant<Types...>* pv)

template <std::size_t I, class... Types> constexpr std::add_pointer_t< const std::variant_alternative_t<I, variant<Types...>> > get_if(const std::variant<Types...>* pv)

(2)

(since C++17)

template <class T, class... Types> constexpr std::add_pointer_t<T> get_if(variant<Types...>* pv)

template <class T, class... Types> constexpr std::add_pointer_t<const T> get_if(const variant<Types...>* pv)

1%29基于索引的非抛出访问器:如果pv不是空指针,并且pv->index() == I的变量中存储的值的指针。pv否则,返回空指针值。如果调用不正确,则为I变量中的有效索引,或者T_I是%28可能是cv-合格%29类型void...

基于2%29类型的非抛出访问器:等效于%281%29I的零基索引。TTypes......打电话是不正确的,如果T的唯一元素Types...或者如果T_I是%28可能是cv-合格%29类型void...

参数

I

-

index to look up

Type

-

unique type to look up

pv

-

pointer to a variant

返回值

指向错误时存储在指向变量或空指针中的值的指针。

例外

1,2%29

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#include <variant>
#include <iostream>
 
int main()
{
    std::variant<int, float> v{12};
 
    if(auto pval = std::get_if<int>(&v))
      std::cout << "variant value: " << *pval << '\n'; 
    else 
      std::cout << "failed to get value!" << '\n'; 
}

二次

产出:

二次

代码语言:javascript
复制
variant value: 12

二次

另见

std::get(std::variant) (C++17)

reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券