Namespaces
Variants

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

From cppreference.com
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)    
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
constexpr std::size_t index() const noexcept;
(since C++17)

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

If the variant is valueless_by_exception, returns variant_npos.

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';
}

Output:

v.index = 1
v.index = 0

See also

checks if a variant currently holds a given type
(function template) [edit]
reads the value of the variant given the index or the type (if the type is unique), throws on error
(function template) [edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.