varint index

C++
template <typename> struct tag { };

template <typename T, typename V>
struct variant_index;

template <typename T, typename... Ts> 
struct variant_index<T, std::variant<Ts...>>
    : std::integral_constant<size_t, std::variant<tag<Ts>...>(tag<T>()).index()>
{ };

// Use as: variant_index<TYPE, VARIANT>::value
Source

Also in C++: