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

std::holds_alternative

Defined in header <variant>

?

?

template <class T, class... Types> constexpr bool holds_alternative(const std::variant<Types...>& v)

?

(since C++17)

检查变体是否v持有另一种选择T...打电话是不正确的,如果T不止一次出现在Types...

参数

v

-

variant to examine

返回值

true如果变体当前持有可供选择的T,,,false否则。

例外

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#include <variant>
#include <string>
#include <iostream>
int main()
{
    std::variant<int, std::string> v = "abc";
    std::cout << std::boolalpha
              << "variant holds int? "
              << std::holds_alternative<int>(v) << '\n'
              << "variant holds string? "
              << std::holds_alternative<std::string>(v) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
variant holds int? false
variant holds string? true

二次

另见

index

returns the zero-based index of the alternative held by the variant (public member function)

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。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com