What is standard defer/finalizer implementation in

2019-02-07 07:48发布

General idea of Golang-style defer is explained here and here.

I wonder, does STL (of C++11, C++14, ...) or maybe Boost or maybe some other library contain implementation of such a class? So I could just use it without reimplementing it in every new project.

标签: c++ boost go stl raii
7条回答
Root(大扎)
2楼-- · 2019-02-07 08:23

I presented a header-only implementation of Go-style defer at CppCon 2014 (YouTube link); I called it Auto. IMHO this is still far and away the best alternative out there in terms of teachability, efficiency, and absolute fool-proofness. In use, it looks like this:

#include "auto.h"

int main(int argc, char **argv)
{
    Auto(std::cout << "Goodbye world" << std::endl);  // defer a single statement...
    int x[4], *p = x;
    Auto(
        if (p != x) {  // ...or a whole block's worth of control flow
            delete p;
        }
    );
    if (argc > 4) { p = new int[argc]; }
}

The implementation looks like this:

#pragma once

template <class Lambda> class AtScopeExit {
  Lambda& m_lambda;
public:
  AtScopeExit(Lambda& action) : m_lambda(action) {}
  ~AtScopeExit() { m_lambda(); }
};

#define Auto_INTERNAL2(lname, aname, ...) \
    auto lname = [&]() { __VA_ARGS__; }; \
    AtScopeExit<decltype(lname)> aname(lname);

#define Auto_TOKENPASTE(x, y) Auto_ ## x ## y

#define Auto_INTERNAL1(ctr, ...) \
    Auto_INTERNAL2(Auto_TOKENPASTE(func_, ctr), \
                   Auto_TOKENPASTE(instance_, ctr), __VA_ARGS__)

#define Auto(...) Auto_INTERNAL1(__COUNTER__, __VA_ARGS__)

Yes, that's the entire file: just 15 lines of code! It requires C++11 or newer, and requires your compiler to support __COUNTER__ (although you can use __LINE__ as a poor man's __COUNTER__ if you need portability to some compiler that doesn't support it). As for efficiency, I've never seen GCC or Clang generate anything other than perfect code for any use of Auto at -O2 or higher — it's one of those fabled "zero-cost abstractions."

The original source also has a C89 version that works on GCC by exploiting some very GCC-specific attributes.

查看更多
Animai°情兽
3楼-- · 2019-02-07 08:25

used like this:

int main()   {
    int  age = 20;
    DEFER { std::cout << "age = " << age << std::endl; };
    DEFER { std::cout << "I'll be first\n"; };
}

My implementation( please add header files yourself):

 class ScopeExit
    {
    public:
    ScopeExit() = default;

    template <typename F, typename... Args>
    ScopeExit(F&& f, Args&&... args)
    {
        // Bind all args, make args list empty
        auto temp = std::bind(std::forward<F>(f), std::forward<Args>(args)...);

        // Discard return type, make return type = void, conform to func_ type
        func_ = [temp]() { (void)temp(); };
    }

    ScopeExit(ScopeExit&& r)
    {
        func_ = std::move(r.func_);
    }

    // Destructor and execute defered function
    ~ScopeExit()
    {
        if (func_)
            func_();
    }

    private:
        std::function< void ()> func_;
    };

    // Ugly macro, help
    #define CONCAT(a, b) a##b

    #define DEFER  _MAKE_DEFER_HELPER_(__LINE__)
    #define _MAKE_DEFER_HELPER_(line)   ScopeExit    CONCAT(_defer, line) = [&] () 
查看更多
Root(大扎)
4楼-- · 2019-02-07 08:31

Before new standard comes, I use simple RAII class for this:

struct ScopeGuard {
    typedef std::function< void() > func_type;
    explicit ScopeGuard(func_type _func) : func(_func), isReleased(false) {}
    ~ScopeGuard() {
        if( !isReleased && func ) try {
            func();
        }catch(...) {};
    }
    void Forget() { isReleased=true; }

    //noncopyable
    ScopeGuard(const ScopeGuard&) = delete;
    ScopeGuard& operator=(const ScopeGuard&) = delete;

private:
    func_type func;
    bool isReleased;
};

Later it can be used for any things, for example:

FILE *fp = fopen(filename.c_str(),"w");
if(fp==NULL) throw invalid_argument();
ScopeGuard _fp_guard([&fp]() {
    fclose(fp);
});

Also, you can use Boost.ScopeExit and similar implementations.

查看更多
聊天终结者
5楼-- · 2019-02-07 08:32

Here's my solution, which is similar to the type you'd encounter in swift, but I don't handle any exceptions (easy enough to add if required, just use a try/catch block like in PSIAlt's solution):

class Defer {
    using F = std::function<void(void)>;
    std::vector<F> funcs;
    void add(F f) {
        funcs.push_back(f);
    }
public:
    Defer(F f) { add(f); }
    Defer() {}
    Defer(const Defer& ) = delete;
    Defer& operator= (const Defer& ) = delete;

    void operator() (F f) { add(f); }
    ~Defer() {
        for(;!funcs.empty();) {
            funcs.back()();
            funcs.pop_back();
        }
    }
};

It may seem clunky due to its use of vector, but it retains the behavior of swift's defer where the functions are called in reverse order:

Defer defer([]{std::cout << "fourth" << std::endl;});
std::cout << "first" << std::endl;
auto s = "third";
defer([&s]{std::cout << s << std::endl;});
std::cout << "second" << std::endl;

But it's a bit more powerful than defer in that you can add deferred calls at any scope lower than your own. You just have to be careful that the lambda captured doesn't go out of scope when you use this (a bit of a flaw but not a serious one if you're careful).

I normally wouldn't use this, unless it's going to be a one-off statement. Ideally you'd wrap any resource around a new class that deallocates it when it goes out of scope, but if its top-level code with lots of resources and error handling, defer does make more sense from a code readability standpoint. You don't have to remember a bunch of new classes that really all do the same thing.

查看更多
闹够了就滚
6楼-- · 2019-02-07 08:37

There is a proposal for std::unique_resource_t which will enable code like

auto file=make_unique_resource(::fopen(filename.c_str(),"w"),&::fclose);

for resources, and it defines a general scope_exit, which should be the same as defer:

// Always say goodbye before returning,
auto goodbye = make_scope_exit([&out]() ->void
{
out << "Goodbye world..." << std::endl;
});

It will be considered for likely adoption in the Post-C++17 standard.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-02-07 08:39

Here is my defer implementation, but without noexcept guarantee, I still don't think it is very good implementation.

Used like this:

#include <iostream>
#include "defer.hpp"

using namespace std;

int main() {
    defer []{cout << "defered" << endl;};
}

Implementation:

#define DEFER_CONCAT_IMPL(x, y) x##y
#define DEFER_CONCAT(x, y) DEFER_CONCAT_IMPL(x, y)
#define AUTO_DEFER_VAR DEFER_CONCAT(__defer, __LINE__)
#define defer ::__defer AUTO_DEFER_VAR; AUTO_DEFER_VAR-

class __defer {
    public:
        template<typename Callable>
            void operator- (Callable&& callable) {
                defer_ = std::forward<Callable>(callable);
            }

        ~__defer() {
            defer_();
        }
    private:
        std::function<void(void)> defer_;
};
查看更多
登录 后发表回答