std::variant<Types...>::index

Returns the zero-based index of the alternative that is currently held by the variant.

# Declarations

constexpr std::size_t index() const noexcept;

(since C++17)

# Example

#include <iostream>
#include <string>
#include <variant>
 
int main()
{
    std::variant<int, std::string> v = "abc";
    std::cout << "v.index = " << v.index() << '\n';
    v = {};
    std::cout << "v.index = " << v.index() << '\n';
}

# See also