我有以下 SSCCE:
#include <iostream>
#include <string>
void foo(const std::string &a) {
std::cout << a << std::endl;
}
template <typename... Args>
void bar(Args &&... args) {
[&]() {
[&]() {
foo(args...);
}();
}();
}
int main() {
const std::string x("Hello World!");
bar(x);
}
在 clang++ (3.9.1) 下编译并发出“Hello World”。 Gcc 6.3 在 -O3
下因段错误而失败。
我可以通过引用显式传递指针和包来解决问题,将 [&]()
替换为 [&args...]()
。但是,到目前为止,我认为 [&]
与将所有参数一一列出一样。
那么这里出了什么问题?
附注:
这不限于 -O3
。 -O0
没有段错误但没有返回预期的结果(“Hello World!”):
[:~/tmp] $ g++-6 -std=c++1z param.cpp && ./a.out
[:~/tmp] $
P.P.S:进一步降低 SSCCE。现在我什至不再使用 -Wall -Wextra
进行诊断了。
最佳答案
我强烈怀疑存在 g++ 错误。
这里有一些注意事项:
- 用任何基本类型替换
std::string
,例如int
still does not work - clang和 VC++将按预期工作
- 不通过引用传递参数包会导致 internal compiler error with g++ 7.0.1输出如下:
internal compiler error: in make_decl_rtl, at varasm.c:1304
...
Please submit a full bug report, with preprocessed source if appropriate.
Please include the complete backtrace with any bug report. See http://gcc.gnu.org/bugs.html for instructions.
关于c++ - GCC 导致 lambda 捕获的参数包的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41769851/