samedi 22 septembre 2018

Segfault when capturing this in template class

While learning C++11 features I've noticed that this code crashes with segfault:

#include <iostream>
#include <functional>

using namespace std;

template <int X = 42>
struct Test {
    int x = X;
    void printer() {
        cout << "this value: " << x <<  endl;
    }
    std::function<void()> lambda = [this] {printer();};
};


int main()
{
    Test<> t;
    t.lambda();
    return 0;
}

If I make Test a regular struct instead of template it works. Why is that? Os: Ubuntu 16.04, compiler : g++ 6.3

Aucun commentaire:

Enregistrer un commentaire