Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call function when leaving scope

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 general as writing a custom class for this.

#include <iostream>
#include <functional>

using namespace std;

class DoInDtor
{
public:
    typedef function<void()> F;
    DoInDtor(F f) : f_(f) {};
    ~DoInDtor() { f_(); }
private:
    F f_;
};

void foo()
{
    DoInDtor byeSayerCustom([](){ cout << "bye\n"; });

    auto cond = true; // could of course also be false
    if ( cond )
        return;

    return;
}

int main()
{
    foo();
}

Sure, one could abuse std::unique_ptr and its custom deleter, but since I am not really acquiring a resource here, that does not sound great to me either in terms of code readability. Any suggestions?

like image 321
Tobias Hermann Avatar asked Mar 26 '14 09:03

Tobias Hermann


2 Answers

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.

like image 152
Craig Scott Avatar answered Oct 21 '22 05:10

Craig Scott


Using a constructor/destructor in this way is a common way to solve this sort problem. I have used it for both locks (constructor takes lock, destructor releases it) and logging purposes (constructor prints something on construction, and destructor prints on destruction, giving a nice callgraph of a project - in the latter case, using macros to also get __FILE__ and __LINE__ store in the object, so we can see where the constructor was called [it's almost impossible to do this for the destructor, but typically it's possible to see the constructor and make out where the destructor gets called]).

like image 2
Mats Petersson Avatar answered Oct 21 '22 04:10

Mats Petersson