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
例
二次
#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';
}二次
产出:
二次
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) | 
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

