Difference between revisions of "cpp/utility/variant/holds alternative"
From cppreference.com
(Change wording according to standard. T has to appear exactly once in Types..., otherwise the call is ill-formed) |
(fmt) |
||
Line 4: | Line 4: | ||
{{dcl header | variant}} | {{dcl header | variant}} | ||
{{dcl | since=c++17 | | {{dcl | since=c++17 | | ||
− | template <class T, class... Types> | + | template< class T, class... Types > |
− | constexpr bool holds_alternative(const std::variant<Types...>& v) noexcept; | + | constexpr bool holds_alternative( const std::variant<Types...>& v ) noexcept; |
}} | }} | ||
{{dcl end}} | {{dcl end}} |
Revision as of 15:04, 29 October 2019
Defined in header <variant>
|
||
template< class T, class... Types > constexpr bool holds_alternative( const std::variant<Types...>& v ) noexcept; |
(since C++17) | |
Checks if the variant v
holds the alternative T
. The call is ill-formed if T
does not appear exactly once in Types...
Contents |
Parameters
v | - | variant to examine |
Return value
true
if the variant currently holds the alternative T
, false
otherwise.
Example
Run this code
#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'; }
Output:
variant holds int? false variant holds string? true
See also
returns the zero-based index of the alternative held by the variant (public member function) | |
(C++17) |
reads the value of the variant given the index or the type (if the type is unique), throws on error (function template) |