Search code examples
c++c++17variadic-templatesfold-expression

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: '...'.

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. What am I missing?

Edit: I'm compiling with the /std:c++17 flag. Trying /std:latest did not help.


Solution

  • Reported as a possible compiler bug: Bug report

    Edit: This is a confirmed bug and a fix has been released in 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 + ...));