#include <iostream> using namespace std; #include <functional> template <class F> class ScopeExitFunction { public: ScopeExitFunction(F& func) throw() : m_func(func) { } ScopeExitFunction(F&& func) throw() : m_func(std::move<F>(func)) { } ScopeExitFunction(ScopeExitFunction&& other) throw() : m_func(std::move(other.m_func)) { // other.m_func = []{}; } ~ScopeExitFunction() throw() { m_func(); } private: F m_func; }; int main() { { std::function<void()> lambda = [] { cout << "called" << endl; }; ScopeExitFunction<decltype(lambda)> f(lambda); ScopeExitFunction<decltype(lambda)> f2(std::move(f)); } return 0; }
without uncommenting this line // other.m_func = []{}; the program produces this conclusion:
Program execution .... $ demo is called terminate called after throwing an instance of 'std :: bad_function_call' what (): bad_function_call
Is it normal that std :: function does not reset its internal function when moving?
source share