http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55532

             Bug #: 55532
           Summary: Runtime segfault calling mutable lambda wrapped in a
                    non-mutable lambda within a template function
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: akram...@gmail.com


The following code segfaults at runtime when we call fun2.

Compiled with "g++-4.7 -std=c++11 -o main main.cpp"

Code:

struct Foo {
    void doit() {
    }
};

template<typename T>
void oops(Foo &foo, const T &) {
    auto fun = [&] () mutable {
        foo.doit();
    };
    auto fun2 = [=]() {
        fun();
    };
    fun2();
}

int main() {
    Foo foo;
    oops(foo, 1);
}

Note:  if we make it non-templated function, we get a compiler error which
correctly detects the mutable/non-mutable disparity.

main.cpp:11:13: error: passing ‘const oops(Foo&, const int&)::<lambda()>’ as
‘this’ argument of ‘oops(Foo&, const int&)::<lambda()>’ discards qualifiers
[-fpermissive]

Reply via email to