(Un)portable defer in C

15 points by janerik


lorddimwit

I always liked the old for-loop trick, which occupies a similar semantic space and is portable (though of course not the Intended Use and is trying to make C something it isn’t).

for (FILE *foo = fopen(“foo.txt”, “r”); foo; fclose(foo), foo = NULL) {
…
}
bwbuhse

Neat. Defer in Zig has been something I’m gotten super used to and it makes things a lot easier.

pekkavaa

I did use both "defer" and GCC's nested functions in a bounding volume hierarchy builder (this one). I felt really clever. Once was enough and I never had to think about it again.

ogoffart

Thanks, but I'll continue using C++ and its destructors (Or Rust) ;-)