Is it legal C++ (this means without undefined behavior, etc.) to set a std::function variable to nullptr from the function/lambda expression stored to this variable when it was invoked?
My intention is to omit further invoking the of std::function.
#include <functional>
std::function<void(int)> func;
void setFunc(std::function<void(int)> f) {
func = f;
}
int main() {
setFunc([](int status) {
// do something...
setFunc(nullptr); // callback shall only be called once
});
for(;;) {
int status = 0;
// fetch status
if (func)
func(status);
}
}