考虑这个例子:
#include <iostream> #include <functional> // std::function #include <vector> // std::vector #include <algorithm> // std::for_each int main(){ auto adder = [](int x) { return [&](int y) { return x+=y; }; }; std::vector < std::function<int(int)> > vec; vec.push_back(adder(1)); vec.push_back(adder(10)); std::for_each(vec.begin(),vec.end(),[](std::function<int(int)> f){std::cout << f(33) << " ";}); std::cout << std::endl; }
一个期望整数34和43 43和76,但gcc 4.6.0产生“内部编译器错误:分段错误”.代码有什么问题?
编辑:here讨论了其他几个例子.