简体   繁体   English

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

[英]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.

相关问题 VS2017不支持二进制折叠? - VS2017 does not support binary fold? 从VS2010迁移到VS2017时,为什么窗口几何形状会发生变化? - Why does Window geometry change when migrating from VS2010 to VS2017? C ++:为什么这个constexpr模板函数在VS2017中导致内部错误?(在gcc中可以) - C++: Why does this constexpr template function cause an internal error in VS2017?(In gcc is ok) VS2017中的_crtBreakAlloc - _crtBreakAlloc in VS2017 VS2017协程为什么不能返回void? - Why can't VS2017 coroutines return void? 为什么checked_array_iterator在没有包含的情况下工作 <iterator> 在VS2013但在VS2017中失败了? - Why does checked_array_iterator work without including <iterator> in VS2013 but fails in VS2017? 如何在 VS2017 中使用 C++ 获取异常跟踪堆栈 - How to get exception trace stack with C++ in VS2017 VS2017为什么对使用输入类型定义但使用原始定义的正向声明的函数发出警告“找不到函数定义”? - Why does VS2017 warn “Function definition not found” for functions forward declared with input typedef'd but defined with original? 为VS2017构建OpenCascade - Build OpenCascade for VS2017 为什么我的功能模板专业化被VS2017而不是VS2015拒绝? - Why is my function template specialization rejected by VS2017 and not by VS2015?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM