【发布时间】:2018-12-06 12:35:40
【问题描述】:
巴里给了我们this gorgeous get_index for variants:
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()>
{ };
按如下方式使用:
using V = variant<A, B, C>;
constexpr const size_t N = get_index<B, V>::value; // 1
它在 Clang (OSX) 中运行良好。
但在 Visual Studio 2017 I'm getting 中:
<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
我不明白为什么。有什么想法吗?
(完全披露:在我的项目中,我实际上使用的是 mpark::variant,因为我一直在使用 Xcode 9,它没有 std::variant。但是,你可以从上面的 Godbolt MCVE 中看到这个也会影响std::variant 的实现。我确信问题出在上面的代码中,或者在编译器中。)
【问题讨论】:
-
代码没问题。编译器错误。
-
一种解决方法是将表达式传送到变量模板中。
-
@Barry 这是一个不同的错误,因为
{}}包含一个不匹配的} -
@Oktalist Ooooooooooooooooooooooooooooos
-
FWIW, it works 如果表达式被移动到
constexpr类型的std::size_t()函数中,但不是constexpr auto函数(编译器无意义地声称使用了auto函数在定义之前)。
标签: c++ visual-studio visual-studio-2017 c++17 variant