[英]Using fold expression to initialize static constexpr class data member doesn't compile
I am confused about a particular piece of code that won't compile even though very similar pieces of code do compile. 我很困惑,即使编译非常相似的代码片段,也无法编译的特定代码片段。
This will not compile: 这不会编译:
#include <bitset>
template<std::size_t ...GROUPS>
class Foo
{
static constexpr std::size_t BIT_COUNT = (GROUPS + ...);
using Bits = std::bitset<BIT_COUNT>;
Bits bits;
};
class Bar : public Foo<6, 6, 6, 6>{};
With the enlightening error 1>c:\\...\\source.cpp(5): error C2059: syntax error: '...'
. 带有启发性错误
1>c:\\...\\source.cpp(5): error C2059: syntax error: '...'
。
This compiles: 这编译:
#include <bitset>
template<std::size_t ...GROUPS>
class Foo
{
using Bits = std::bitset<(GROUPS + ...)>;
Bits bits;
};
class Bar : public Foo<6, 6, 6, 6>{};
This also compiles: 这也编译:
#include <bitset>
template<auto... t>
constexpr auto static_sum()
{
return (t + ...);
}
template<std::size_t ...GROUPS>
class Foo
{
static constexpr std::size_t BIT_COUNT = static_sum<GROUPS...>();
using Bits = std::bitset<BIT_COUNT>;
Bits bits;
};
class Bar : public Foo<6, 6, 6, 6>{};
I'm compiling with MSVC++ in Visual studio 15.9.8. 我正在使用Visual Studio 15.9.8中的MSVC ++进行编译。 What am I missing?
我错过了什么?
Edit: I'm compiling with the /std:c++17
flag. 编辑:我正在使用
/std:c++17
标志进行编译。 Trying /std:latest
did not help. 尝试
/std:latest
没有帮助。
Reported as a possible compiler bug: Bug report 报告为可能的编译器错误: 错误报告
Edit: This is a confirmed bug and a fix has been released in Visual Studio 2019. 编辑:这是一个确认的错误,并在Visual Studio 2019中发布了一个修复程序。
I've also slightly simplified my final solution to the following: 我还略微简化了我对以下内容的最终解决方案:
static constexpr std::size_t BIT_COUNT = [](int i) { return i; }((GROUPS + ...));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.