6 ms·
No need to box with std::function. Since C++17 this is enough: template<class Fn> struct defer { Fn fn; ~defer() { fn(); } }; template<class Fn
by gpderetta 6y ago
No need to box with std::function. Since C++17 this is enough:
template<class Fn>
struct defer {
Fn fn;
~defer() { fn(); }
};
template<class Fn>
defer(Fn) -> defer<Fn>;
int main() {
defer _ { [] { std::cout << "exit"; }};
}
Although you might want to add the ability to dismiss() the guard.
edit: a more complete implementation: https://godbolt.org/z/6675K1 https://godbolt.org/z/6675K1 . I like to name my guards. Making them anonymous is left as an exercise for the reader.
- knorker 6y agoYeah, I chose not to templatize and add dismiss() mostly to make it fit better in a comment. But like I said at some point, if it gets complicated, then probably the value should be properly wrapped, with actual owner semantics rather than defers.
- gpderetta 6y agoYou are right in most cases. I find it useful for transactional code though, where I might be modifying a bunch of data structures and I want to undo everything on error. Doesn't really make much sense to write classes in this case if the code is really function specific.