std::holds_alternative
STD::搁置[医]替代
Defined in header | | |
---|---|---|
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。