std::holds_alternative - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::holds_alternative

From cppreference.com
< cpp‎ | utility‎ | variant

Defined in header `<variant>` |  |
template <class T, class... Types> constexpr bool holds_alternative(const std::variant<Types...>& v) |  | (since C++17)

Checks if the variant `v` holds the alternative `T`. The call is ill-formed if `T` appears more than once in `Types...`

### [edit] Parameters

v | - | variant to examine

### [edit] Return value

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

### [edit] Exceptions

`noexcept` specification:
noexcept

### [edit] 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

### [edit] See also

index | returns the zero-based index of the alternative held by the variant (public member function) [edit]
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) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/holds_alternative&oldid=91929"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template