简体   繁体   English

使用fold表达式来初始化静态constexpr类数据成员不会编译

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

相关问题 在模板化类中初始化静态constexpr成员 - Initialize a static constexpr member in templated class 初始化 class 模板的 static constexpr 成员变量 - Initialize static constexpr member variable of class template 使用派生类的静态constexpr数据成员初始化基类的静态constexpr数据成员 - Initializing a static constexpr data member of the base class by using a static constexpr data member of the derived class 在类模板中使用条件运算符初始化静态constexpr char数组成员 - Initialize static constexpr char array member with conditional operator in class template 我可以在 class 之外初始化一个“constexpr static”成员吗? - Can I initialize a `constexpr static` member outside the class? 在编译时使用constexpr初始化指向成员函数的指针数组 - Initialize array of pointers to member functions at compile time using constexpr 使用表达式找不到模板化的静态constexpr成员函数 - static constexpr member function in templated using expression not found std::enable_if 基于使用静态 constexpr 成员函数的表达式 - std::enable_if based on expression using static constexpr member function 为什么我需要在constexpr类中成员初始化非静态数组成员? - Why do I need to member-initialize a non-static array member in a constexpr class? 一个类不能有自己的静态 constexpr 成员实例吗? - Can't a class have static constexpr member instances of itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM