2 Comments
5dEdited

Nice try, but [STC](https://github.com/stclib/STC) 's defer/with/scope is possibly the most beautiful, and simplest, implementation I've ever seen.

[Docs](https://github.com/stclib/STC/blob/master/docs/algorithm_api.md#raii-scope-macros)

[Src](https://github.com/stclib/STC/blob/master/include/stc/common.h)

``` c

#define c_defer(...) \

for (int _i = 1; _i; _i = 0, __VA_ARGS__)

// see also c_with, c_scope, with ideal semantics and similar impl

```

Expand full comment

If all you need is basic defer I wouldn't do the horrible monstrosity I did in the article, the for-loop trick does the job and does it well.

The goal was to implement something like N3199 using just preprocessor abuse, meaning defers anywhere in the block and the ability to return out of the function.

Please do *not* use this mess I wrote in production 😅.

Expand full comment