相关疑难解决方法(0)

为什么VS2017上的get_index实现失败?

巴里给我们这个华丽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)

c++ variant visual-studio c++17 visual-studio-2017

6
推荐指数
1
解决办法
232
查看次数

标签 统计

c++ ×1

c++17 ×1

variant ×1

visual-studio ×1

visual-studio-2017 ×1