考虑以下代码片段:
template <int... Is>
void foo()
{
([i = Is]{}(), ...);
}
clang++
(trunk) 使用-std=c++17
成功编译代码
g++
(trunk) 编译失败,错误如下:<source>: In function 'void foo()': <source>:4:11: error: parameter packs not expanded with '...': ([i = Is]{}(), ...); ^~ <source>:4:11: note: 'Is' <source>:4:16: error: operand of fold expression has no unexpanded parameter packs ([i = Is]{}(), ...); ~~~~~~~~~~^~
这是一个 g++
错误,还是标准阻止将参数包扩展为 lambda-introducer 的一部分?
最佳答案
这看起来像是一个错误。
A pack expansion consists of a pattern and an ellipsis, the instantiation of which produces zero or more instantiations of the pattern in a list (described below). The form of the pattern depends on the context in which the expansion occurs. Pack expansions can occur in the following contexts:
- ... [not relevant]
- In a fold-expression; the pattern is the cast-expression that contains an unexpanded parameter pack.
带有函数调用的完整 lambda 表达式(如您所见)是有效的 cast-expression如果有人遵循语法产生式。没有理由将其排除在有效模式之外。
关于c++ - 在折叠表达式中扩展参数包作为 lambda 捕获的一部分 - gcc 与 clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49722969/