std::variant<Types...>::index - cppreference.com (original) (raw)
| 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.
[edit] Example
#include #include #include 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:
[edit] 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] |