Call a function before function exits

前端 未结 5 793
抹茶落季
抹茶落季 2021-01-28 21:49

I will begin with an example. Suppose I need to guard a code with a function inside a mutex. There are two ways of implementing this.

#include 
#         


        
5条回答
  •  [愿得一人]
    2021-01-28 22:20

    You could use something like ScopeGuard. (Now somewhat old-fashioned.)

    But given how easy and clear it is to construct a specific RAII wrapper for each resource type I would normally do that.

    (I don't think boost ever adopted anything like ScopeGuard. With std::function, lambdas and so on it's easy to do your own.)

提交回复
热议问题