4

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.

Jupiter
  • 1,421
  • 2
  • 12
  • 31
  • 2
    Compiler error, please report. – Barry Mar 12 '19 at 21:11
  • 2
    Compile fine here with gcc/clang [Demo](http://coliru.stacked-crooked.com/a/dcc611fedec8b576) – Jarod42 Mar 12 '19 at 21:11
  • 2
    So probably a msvc bug. – Jarod42 Mar 12 '19 at 21:13
  • Support for C++17 fold expressions in Visual Studio was put off for a long time (partly because it was buggy on first release, like this). Rest assured it works correctly iff it compiles, so there's no chance for false positives. Try setting your C++ standard for the project to C++17 or Latest, or the like and see if those newer standards fix it. – Cruz Jean Mar 12 '19 at 21:38
  • I'm sorry I forgot to mention. I'm compiling with the `/std:c++17` already. I've also tried `/std:latest` to no avail. Updated my question. – Jupiter Mar 12 '19 at 21:57

1 Answers1

4

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 + ...));
Jupiter
  • 1,421
  • 2
  • 12
  • 31