C++11 scope exit guard, a good idea?

前端 未结 10 818
囚心锁ツ
囚心锁ツ 2020-12-08 05:00

I\'ve written a small utility class for C++11 which I use as a scope guard for easier handling of exception safety and similar things.

Seems somewhat like a hack. Bu

10条回答
  •  情书的邮戳
    2020-12-08 05:27

    We could omit the ugly [&] stuff by putting it in the define:

    #define UTILITY_SCOPE_EXIT(f) const auto& _UTILITY_EXIT_SCOPE_LINENAME(EXIT, __LINE__) = ::detail::create_scope_exit([&]f)
    

    Then:

    UTILITY_SCOPE_EXIT({myfile.close();});
    

    Tested with MSVC++ 11.0 (VS2012). Regards.

提交回复
热议问题