C++
应用 | Utilities

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

二次

另见

indexreturns 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。

http://en.cppreference.com/w/cpp/实用程序/变体/持有[医]替代