【发布时间】:2013-07-13 11:47:42
【问题描述】:
我错过了什么?以下代码使用 g++ 按预期编译。
#include <functional>
#include <iostream>
#include <string>
using namespace std;
typedef std::function<void()> Foo;
/* This function does NOT make g++ segfault */
Foo factory() {
return [] {cout << "Hi!" << endl;};
};
int main() {
/* This nested lambda makes g++ segfault */
// function<Foo()> factory = [] {
// return [] {cout << "Hello!" << endl;};
// };
factory()();
return 0;
}
使用的编译标志:
g++ -c -Wall -std=c++0x NestedLambdaProblem.cpp
如果我取消注释在 main 中用 // 注释掉的三行,编译器会出现这样的段错误
$ g++ -c -Wall -std=c++0x NestedLambdaProblem.cpp
g++: internal compiler error: Segmentation fault (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.6/README.Bugs> for instructions.
关于使用的g++版本:
$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
【问题讨论】:
-
这不是你的错,而是编译器的问题。它与 g++ 4.7.2 ideone.com/SRSYJJ 一起运行
-
你不需要捕获
cout吗? -
在 msvc 11 中运行良好:-P
-
@John Dibling:不,
cout不是局部变量,而是“全局”(在命名空间std中)对象,并且 lambdas 不需要捕获全局对象。 -
他不是在推动它们,而是在他的样本中轻轻地触摸它们..
标签: c++ c++11 lambda g++ segmentation-fault