#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;
}
この行のコメントを外さない// other.m_func = []{};
と、プログラムは次の出力を生成します。
プログラムを実行しています.... $demo called terminate called after throwing an instance of 'std::bad_function_call' what(): bad_function_call
移動時に std::function が内部関数をリセットしないのは正常ですか?