巴里给我们这个华丽get_index的变种:
template <typename> struct tag { };
template <typename T, typename V>
struct get_index;
template <typename T, typename... Ts>
struct get_index<T, std::variant<Ts...>>
: std::integral_constant<size_t, std::variant<tag<Ts>...>(tag<T>()).index()>
{ };
Run Code Online (Sandbox Code Playgroud)
使用如下:
using V = variant<A, B, C>;
constexpr const size_t N = get_index<B, V>::value; // 1
Run Code Online (Sandbox Code Playgroud)
它在Clang(OSX)中运行良好.
但在Visual Studio 2017中,我得到以下内容:
<source>(10): error C2039: 'index': is not a member of 'std::variant<tag<Ts>...>'
<source>(10): note: see declaration of 'std::variant<tag<Ts>...>'
<source>(11): note: see reference to class template instantiation 'get_index<T,std::variant<_Types...>>' being compiled …Run Code Online (Sandbox Code Playgroud)