这是明确定义的行为吗?
#include <functional>
void foo() {
auto f = new std::function<void()>;
*f = [f]() { delete f; };
(*f)();
f = nullptr;
}
int main() {
foo();
}
Run Code Online (Sandbox Code Playgroud)
使用最新的g ++,如果我在模板中执行此操作,它会在valgrind下运行时导致无效读取,否则它可以正常工作.为什么?这是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>();
}
Run Code Online (Sandbox Code Playgroud)