    #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()
    {
        for(;;)
        {
            DoInDtor byeSayerCustom([](){ cout << "bye\n"; });
            auto cond = true; // could of course also be false sometimes
            if (cond)
                break;
        }
        // ...
        return;
    }

    int main()
    {
        foo();
    }