C: Simple Defer, Ready to Use
gustedt.wordpress.com
C: Simple Defer, Ready to Use
1–10 of 157 posts
Re: C: Simple Defer, Ready to Use
#2This should have been in C ages ago, but better late than never!
Re: C: Simple Defer, Ready to Use
#3Very nice, using the goto trick to perform cleanups always felt dirty to me.
Re: C: Simple Defer, Ready to Use
#4Do defers get processed if an exception is thrown? I know C doesn’t have exceptions, but real world C code often interacts with C++ code. So it is necessary imo to define that interaction.
In C++ we have something pretty similar already in the form of Folly ScopeGuard (SCOPE_EXIT {}).
Re: C: Simple Defer, Ready to Use
#5This should have been in C ages ago, but better late than never!
This isn't new. The only C23 feature this article uses is the attribute syntax, but you could just use `__attribute((cleanup(F)))` instead.
Re: C: Simple Defer, Ready to Use
#6Re: C: Simple Defer, Ready to Use
#7Do defers get processed if an exception is thrown? I know C doesn’t have exceptions, but real world C code often interacts with C++ code. So it is necessary imo to define that interaction. In C++ we have something pretty similar already in the form of Folly ScopeGuard (SCOPE_EXIT {}).
GCC has the __cleanup__ attribute which works in conjunction with exceptions in C to provide an RAII mechanism.
Re: C: Simple Defer, Ready to Use
#8Re: C: Simple Defer, Ready to Use
#9[deleted]
Re: C: Simple Defer, Ready to Use
#10I know you can do these things, but I've always tried to avoid macro programming all together in C and C++ unless forced to use them by dependencies in idiomatic usage.