1 2 3 4 5 6 7
| template<typename Lambda> struct Defer : Lambda { ~Defer() { Lambda::operator()(); } };
template<typename Lambda> Defer(Lambda) -> Defer<Lambda>;
|
1 2 3 4
| Defer guard{[sockfd]{ shutdown(sockfd, SHUT_WR); }};
|
This is a short tutorial on implementing golang-style "defer" in C++.
1 2 3
| The favored technique will be: + C++ template and lambda - C macro
|
The Explaination
- Inheriting from a lambda type, we define a template class
Defer
whose dtor calls the Lambda::operator()
.
- Explicitly deduce a ctor
Defer(Lambda)
to the exact type Defer<Lambda>
. This step can be omitted in C++20 or later.
- Define a
Defer
object, initialized with the lambda, at any scope, similar to golang.
Tips and More
- You may need to capture some local variables into the lambda.
-O1
is recommended for the lambda inlining, which makes this idiom zero-overhead.
- Using C macro, this idiom can be developed more concise. Personally I don't like C macro, however, because of safety concerns.
- A library: lizho/FakeGolang