断言中的折叠表达式在某些机器上编译,但在其他机器上不编译

Fold expression within assertion compiles on some machines, but not on others

我有以下代码(出于演示目的)应该使用 C++17 折叠表达式断言所有参数的计算结果为真。

#include <cassert>

template<typename... Ts>
void fn(Ts... ts)
{
    assert(ts && ...);
}

int main()
{
    fn(true, true, true);
    fn(true, true, false, true);
}

在 Coliru 上它按预期工作;它不在我的机器上。我收到错误

In file included from /usr/include/c++/8.2.1/cassert:44,
                 from foldexpr.cpp:1:
foldexpr.cpp: In function ‘void fn(Ts ...)’:
foldexpr.cpp:6:15: error: expected ‘)’ before ‘&&’ token
     assert(ts && ...);
               ^~
foldexpr.cpp:6:5: error: expected ‘;’ before ‘)’ token
     assert(ts && ...);
     ^~~~~~

使用 gcc 版本 8.2.1 20180831。在 Ubuntu 使用 gcc 版本 5.4.0 20160609 我得到

In file included from /usr/include/c++/5/cassert:43:0,
                 from foldexpr.cpp:1:
foldexpr.cpp: In function ‘void fn(Ts ...)’:
foldexpr.cpp:6:18: error: expected primary-expression before ‘...’ token
     assert(ts && ...);
                  ^
foldexpr.cpp:6:18: error: expected ‘)’ before ‘...’ token
foldexpr.cpp:6:22: error: expected ‘)’ before ‘;’ token
     assert(ts && ...);
                      ^
foldexpr.cpp:6:22: error: parameter packs not expanded with ‘...’:
foldexpr.cpp:6:22: note:         ‘ts’

这里是 table 它在相应的编译器版本中有效和无效的地方。

| OS              | GCC               | Clang             |
|-----------------|-------------------|-------------------|
| Arch            | no (v8.2.1)       | no (v7.0.0)       |
| Ubuntu (Coliru) | yes (v8.1.0)      | yes (v5.0.0)      |
| Debian          | yes (v6.3.0)      | -                 |
| ? (Godbolt)     | no (all versions) | no (all versions) |

因为它 works/fails 如此武断,我觉得这是标准库的问题,默认情况下 clang 使用 libstdc++,我相信,这可以解释为什么它对两者都有效或无效一个系统。

这段代码应该编译吗?如果是,我该如何让它发挥作用?如果不是,是否是编译器错误?

PS: 在 Coliru 上我已经能够使用相当复杂的折叠表达式,但我还没有在其他机器上尝试过其他的。

折叠表达式的形式必须是

( pack op ... )
( ... op pack )
( pack op ... op init )
( init op ... op pack ) 

你的

assert(ts && ...)

不遵循那个,它缺少括号。你需要

assert((ts && ...))

使其语法正确。