gpt4 book ai didi

c++ - 使用折叠表达式初始化静态 constexpr 类数据成员不编译

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:49:52 25 4
gpt4 key购买 nike

我对某段代码无法编译感到困惑,即使非常相似的代码段确实可以编译。

这不会编译:

#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>{};

具有启发性错误 1>c:\...\source.cpp(5): error C2059: syntax error: '...'

编译:

#include <bitset>
template<std::size_t ...GROUPS>
class Foo
{
using Bits = std::bitset<(GROUPS + ...)>;
Bits bits;
};

class Bar : public Foo<6, 6, 6, 6>{};

这也编译:

#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>{};

我在 Visual Studio 15.9.8 中使用 MSVC++ 进行编译。我错过了什么?

编辑:我正在使用 /std:c++17 标志进行编译。尝试 /std:latest 没有帮助。

最佳答案

报告为可能的编译器错误:Bug report

编辑:这是一个已确认的错误,并且已在 Visual Studio 2019 中发布修复。

我还将我的最终解决方案略微简化为以下内容:

static constexpr std::size_t BIT_COUNT = [](int i) { return i; }((GROUPS + ...));

关于c++ - 使用折叠表达式初始化静态 constexpr 类数据成员不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55130682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com