std::variant<Types...>::index
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
std::variant
variant::index
(C++26)
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
Run this code
#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
[edit] See also
(C++17)
(function template) [edit]