call function when leaving scope

后端 未结 2 752
栀梦
栀梦 2021-01-23 04:27

What is the most elegant solution for calling a function automatically when leaving a scope? My current approach (see below) works but I guess there should be something more gen

2条回答
  •  无人及你
    2021-01-23 04:35

    Angew and Cassio are pointing you in the right direction here with ScopeGuard. Using std::function can incur dynamic memory allocation and is not the most efficient solution. The ScopeGuard-based implementations avoid that by encoding the type of the function object to invoke into the ScopeGuard class itself as a template parameter. With a helper function template, you never have to explicitly state that type, the compiler deduces it for you (and has to in the case of a lambda being used as the function object to invoke).

    Rather than repeating all the details here, I recently wrote two articles about this very area:

    • Let your compiler do your housekeeping
    • OnLeavingScope: The sequel

    The first article focuses on simplicity and starts from a similar place as your original question, including a motivating example. The second one goes through all the steps to evolve that to an efficient and robust implementation which results in concise, very readable code. The second article also explains how the material presented relates to Andrei's work with ScopeGuard and the C++ standards proposal for scope_exit mentioned by Cassio.

提交回复
热议问题