Reputation: 92
What have I missed? The following code compiles as expected, using 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;
}
The compile flags used:
g++ -c -Wall -std=c++0x NestedLambdaProblem.cpp
If I uncomment the three lines that is commented out with // in main, the compiler segfaults like this
$ 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.
About the g++ version used:
$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Upvotes: 1
Views: 943
Reputation: 70516
If you want to use C++11 features in a production environment, make sure to install the latest stable g++ or Clang compiler. Currently, that would be g++ 4.8.1 and Clang 3.3. They can be installed either from source (somewhat tricky, but if you have ever compiled a Linux kernel e.g., you should manage), or from a binary package from your own Linux distro or from a 3rd party package server.
Very roughly -and unofficially- speaking, you can view g++ 4.7 as a late beta-version with a high-quality of implementation (although even g++ 4.7.2 as subtle lambda bugs), whereas g++ 4.6 is more of alpha / early beta quality for many C++11 features. You happen to have run into a g++ 4.6 bug. There is not much sense in tracking this particular bug down, you may or may not find a similar fault in the bug database. In any case, upgrading to the most recent stable release is the recommended approach.
Current development is gearing towards C++14 support (g++ 4.9 and Clang 3.4), and lambdas are one of the main features under development (to support auto arguments and move capture). Those features are very much experimental now and you can expect some bugs there as well. Caveat emptor (but by all means experiment!).
Upvotes: 2