std::holds_alternative
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
std::variant
(C++26)
holds_alternative
Defined in header
<variant>
template< class T, class... Types >
constexpr bool holds_alternative( const std::variant <Types...>& v ) noexcept;
(since C++17)
constexpr bool holds_alternative( const std::variant <Types...>& v ) noexcept;
Checks if the variant v holds the alternative T
. The call is ill-formed if T
does not appear exactly once in Types...
Contents
[edit] Parameters
v
-
variant to examine
[edit] Return value
true if the variant currently holds the alternative T
, false otherwise.
[edit] Example
Run this code
#include <cassert> #include <string> #include <variant> int main() { std::variant <int, std::string > v = "abc"; assert (not std::holds_alternative<int>(v)); assert (std::holds_alternative<std::string >(v)); }
[edit] See also
(C++17)
(function template) [edit]