On Sat, Feb 28, 2026 at 7:02 AM Andres Freund <[email protected]> wrote:
> I wish msvc implemented something akin to gcc/clang's
> attribute(cleanup(cleanup_function)), but it doesn't look like it
> does. Obviously it would if we were to compile with C++, but I don't think
> anybody has appetite for the work it'd need to get there.
Well, it does have __try/__finally, but the future of the idea for C2y
looks like Go or Zig:
#include <stddefer.h>
p = malloc();
defer free(p);
It's already available in bleeding edge GCC and Clang 22, based on the
TS 25755 draft[1]. Unfortunately for the real world, it's annoyingly
difficult to come up with *nice* looking macros that can expand to the
dialects we'd need to cover even if we were OK with abandoning the
notion of supporting C < 2y compilers other than the 3 we talk about,
due to syntactic structure and semantic differences, presumably:
1. C2y defer
2. C++ RAII
3. GCC/Clang cleanup attributes
4. MSVC __try/__finally
But with big honking framing macros BEGIN_MAGIC_PIXIE_DUST(), ...
pg_defer_pfree(p) (assuming we'd want a style with a pathway to the
future standard?); ... END_MAGIC_PIXIE_DUST(), I think it's probably
doable... There are lots of people trying to suffer through
portable-enough-for-me and just-enough-functionality-for-my-project,
maybe some inspiration[2]...
[1] https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3734.pdf
[2] https://antonz.org/defer-in-c/