GCC为lambda捕获的参数包导致段错误 [英] GCC causes segfault for lambda-captured parameter pack
问题描述
我有以下的SSCCE:
#include< iostream>
#include< string>
void foo(const std :: string& a){
std :: cout<< a<<的std :: ENDL;
}
模板< 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 ...]()
替换 [&]()
。但是,到目前为止,我还是认为 [&]
会和逐个列出所有参数一样。
那么这里出了什么问题?
PS:
这不仅限于 -O3
。 -O0
不会返回预期结果(Hello World!):
[:〜/ tmp] $ g ++ - 6 -std = c ++ 1z param.cpp&& ./a.out
[:〜/ tmp] $
PPS :进一步降低了SSCCE。现在我甚至没有得到 -Wall -Wextra
的诊断结果。
我强烈怀疑 g ++ bug 。
以下是一些注意事项: / p>
- 用任何基本类型替换
std :: string
,例如int
仍然无法使用 - 铿锵和 VC ++ 将按预期工作
- 不通过引用传递参数包导致 内部编译器错误g ++ 7.0.1 ,并输出如下内容:
lockquote
内部编译器错误:in make_decl_rtl,at varasm.c:1304
...
Please
submit a完整的错误报告,如果适用的话还有预处理源。
请包含任何错误报告的完整回溯。有关说明,请参阅
http://gcc.gnu.org/bugs.html 。
I have the following 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); }
Under clang++ (3.9.1) this compiles and emits "Hello World". Gcc 6.3 fails with a segmentation fault under
-O3
.I can fix the problem by explicitly passing the pointer and the pack by reference, replacing
[&]()
with[&args...]()
. However, up to now, I thought that[&]
would do the same as listing all arguments one by one.So what is going wrong here?
P.S: This is not limited to
-O3
.-O0
does not segfault but does not return the expected result ("Hello World!"):[:~/tmp] $ g++-6 -std=c++1z param.cpp && ./a.out [:~/tmp] $
P.P.S: Further reduced SSCCE. Now I don't even get a diagnostic with
-Wall -Wextra
anymore.解决方案I strongly suspect a g++ bug.
Here are some notes:
- replacing
std::string
with any elementary type, e.g.,int
still does not work - clang and VC++ will work just as intended
- not passing parameter pack by reference causes an internal compiler error with g++ 7.0.1 with the following output:
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.
这篇关于GCC为lambda捕获的参数包导致段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- replacing