Here is the code:
#include <vector>
#include <functional>
class TestClass {
public:
const std::vector<int> &getStuff() const {
return callback();
}
protected:
std::function<const std::vector<int> &()> callback = []() {
static const std::vector<int> dummy;
return dummy;
};
};
int main()
{
TestClass test;
const auto& vec = test.getStuff();
vec.size(); // segfault
return 0;
}
I don't understand why it should behave like this. I suspect some "known" rule about lambdas or std::function
s but I cannot seem to find the right keywords for this problem so here I am.
Tested on GCC 11 and Clang 14 Ubuntu.