using Value = variant<int, double, string>;↵
↵
Value values[] = {42, 3.14, string("hello")};↵
↵
for (const auto& v : values) {↵
visit([](const auto& arg) {↵
using T = decay_t<decltype(arg)>;↵
if constexpr (is_same_v<T, int>)↵
cout << "int: " << arg << endl;↵
else if constexpr (is_same_v<T, double>)↵
cout << "double: " << arg << endl;↵
else↵
cout << "string: " << arg << endl;↵
}, v);↵
}↵
↵
cout << "Index of last: " << values[2].index() << endl;