Search code examples
gccinternal

GCC internal error using lambda expressions


Could anyone give me a hint why this code produces an internal compiler error? I've tested it on gcc 4.8.1.

#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>

class Dummy {
private:
    int dummy;
public:
    Dummy() { dummy = 0; }
    ~Dummy() { }
    int getDummy() const { return dummy; }
    void setDummy(int d) { dummy = d; }
};

class DummyCollection {
private:
    std::vector<Dummy> table;

public:
    void eachDummy(std::function<bool (const Dummy& d)>& closure) {
        for(const Dummy& d: table) {
            if(! closure(d))
                break;
        }
    }
};

DummyCollection dc;

void iterateDummies(std::function<bool (const Dummy& d)>& closure) {
    dc.eachDummy([&] (const Dummy& d) {
        return closure(d);
    });
}

int main() {
    iterateDummies([&] (const Dummy& d) {
        std::cout << "dummy " << d.getDummy() << std::endl;
        return true;
    });

    return 0;
}

Here's the compiler output:

(2:514)$ g++ test.cpp -o test -std=c++11
test.cpp: In lambda function:
test.cpp:34:2: internal compiler error: in pop_binding, at cp/name-lookup.c:382
  });
  ^
Please submit a full bug report,
with preprocessed source if appropriate.
See <https://bugs.archlinux.org/> for instructions.

Line 34 is the end of the iterateDummies function. It seems that lambda function can't be called from another lambda function, is this true?


Solution

  • I've reported this to ArchLinux bugzilla and got this answer, so I'm going to paste it here for other googlers' benefit:

    https://bugs.archlinux.org/task/35803

    gcc-4.9-20130324 BAD
    gcc-4.9-20130331 GOOD