2 ms·
It's a short article, and worth reading if you can work out why the Wayback Machine isn't working for you. The magic is basically: static inline void defe
by shakna 7y ago
It's a short article, and worth reading if you can work out why the Wayback Machine isn't working for you.
The magic is basically:
static inline void defer_cleanup(void (^*b)(void)) { (*b)(); }
#define defer_merge(a,b) a##b
#define defer_varname(a) defer_merge(defer_scopevar_, a)
#define defer __attribute__((cleanup(defer_cleanup))) void (^defer_varname(__COUNTER__))(void) =
Which let's you do:
FILE *a = fopen ("a.txt", "r");
if (!a)
return EXIT_FAILURE;
defer
{
fclose (a);
};
It uses cleanup and blocks, both of which are extensions. There may also be some strangeness with the way blocks hold memory. (Block references become const copies).