[英]Why does this get_index implementation fail on VS2017?
Barry gave us this gorgeous get_index
for variants : Barry 为变种提供了这个华丽的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()>
{ };
To be used as follows: 使用如下:
using V = variant<A, B, C>;
constexpr const size_t N = get_index<B, V>::value; // 1
It works great in Clang (OSX). 它在Clang(OSX)中运行良好。
But in Visual Studio 2017 I'm getting the following: 但在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
Compiler returned: 2
I can't see why. 我不明白为什么。 Any ideas? 有任何想法吗?
(Full disclosure: in my project I'm actually using mpark::variant
because I have been using Xcode 9, which didn't have std::variant
. However, you can see from the Godbolt MCVE above that this affects the implementation with std::variant
as well. I'm convinced the problem is either in the code above, or in the compiler.) (完全披露:在我的项目中我实际上使用的是mpark::variant
因为我一直在使用Xcode 9,它没有std::variant
。但是,从上面的Godbolt MCVE可以看出这会影响实现std::variant
。我确信问题出在上面的代码中,或者在编译器中。)
I bet my 2 cents that is a compiler bug. 我打赌我的2美分这是一个编译器错误。
I see that if I write in main()
我看到如果我写在main()
std::cout << std::variant<tag<int>, tag<float>>{tag<float>{}}.index() << std::endl;
the compiler doesn't complain. 编译器没有抱怨。
And doesn't complain also if I write a template function as follows 如果我按如下方式编写模板函数,也不会抱怨
template <typename T, typename ... Ts>
void foo ()
{ std::cout << std::variant<tag<Ts>...>(tag<T>{}).index() << std::endl; }
and I call it, from main()
, with 我从main()
调用它
foo<int, long, int, long long>();
No problem also declaring the following variable in main()
没问题也在main()
声明以下变量
std::integral_constant<std::size_t, std::variant<tag<int>, tag<float>>(tag<float>{}).index()> ic;
But if I change the get_index
specialization as follows (using braces for initialization instead of round parentheses) 但是,如果我更改get_index
如下(使用大括号进行初始化而不是圆括号)
template <typename T, typename... Ts>
struct get_index<T, std::variant<Ts...>>
: std::integral_constant<std::size_t, std::variant<tag<Ts>...>{tag<T>()}.index()>
{ };
the compiler complain but with a different error 编译器抱怨但有不同的错误
example.cpp example.cpp
(12): error C2440: 'initializing': cannot convert from 'initializer list' to 'std::variant...>' (12):错误C2440:'初始化':无法从'初始化列表'转换为'std :: variant ...>'
(12): note: The target type has no constructors (12):注意:目标类型没有构造函数
(13): note: see reference to class template instantiation 'get_index>' being compiled (13):注意:请参阅正在编译的类模板实例化'get_index>'的引用
Compiler returned: 2 编译返回:2
Seems that, for reasons I can't understand, the compiler doesn't see std::variant<tag<Ts>...>
, inside get_index
, as a std::variant
with all it's methods. 似乎由于我无法理解的原因,编译器没有在get_index
看到std::variant<tag<Ts>...>
作为std::variant
及其所有方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.