Merge pull request #90 from john-lynch/master

Fixing move constructor/semantics of Final_act
This commit is contained in:
Neil MacIntosh 2015-09-28 23:15:24 -07:00
commit d023ad51a7
2 changed files with 20 additions and 5 deletions

View File

@ -50,16 +50,17 @@ template <class F>
class Final_act class Final_act
{ {
public: public:
explicit Final_act(F f) : f_(std::move(f)) {} explicit Final_act(F f) : f_(std::move(f)), invoke_(true) {}
Final_act(const Final_act&& other) : f_(other.f_) {} Final_act(Final_act&& other) : f_(std::move(other.f_)), invoke_(true) { other.invoke_ = false; }
Final_act(const Final_act&) = delete; Final_act(const Final_act&) = delete;
Final_act& operator=(const Final_act&) = delete; Final_act& operator=(const Final_act&) = delete;
~Final_act() { f_(); } ~Final_act() { if (invoke_) f_(); }
private: private:
F f_; F f_;
bool invoke_;
}; };
// finally() - convenience function to generate a Final_act // finally() - convenience function to generate a Final_act

View File

@ -37,6 +37,20 @@ SUITE(utils_tests)
CHECK(i == 1); CHECK(i == 1);
} }
TEST(finally_lambda_move)
{
int i = 0;
{
auto _1 = finally([&]() {f(i);});
{
auto _2 = std::move(_1);
CHECK(i == 0);
}
CHECK(i == 1);
}
CHECK(i == 1);
}
TEST(finally_function_with_bind) TEST(finally_function_with_bind)
{ {
int i = 0; int i = 0;