Deleting a std::function object within itself
问题 Is this well defined behavior? #include <functional> void foo() { auto f = new std::function<void()>; *f = [f]() { delete f; }; (*f)(); f = nullptr; } int main() { foo(); } Using the most recent g++, if I do this within a template it causes invalid reads while running under valgrind, otherwise it works fine. Why? Is this a bug in g++? #include <functional> template<std::size_t> void foo() { auto f = new std::function<void()>; *f = [f]() { delete f; }; (*f)(); f = nullptr; } int main() { foo<0