std::holds_alternative

Header: <variant>

Checks if the variant v holds the alternative T. The call is ill-formed if T does not appear exactly once in Types…

# Declarations

template< class T, class... Types >
constexpr bool holds_alternative( const std::variant<Types...>& v ) noexcept;

(since C++17)

# Parameters

# Return value

true if the variant currently holds the alternative T, false otherwise.

# Example

#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));
}

# See also