Nobody knows about BOOST_SCOPE_DEFER? #include BOOST_SCOPE_DEFER [&] { close(fd); }; Implementation here: https://github.com/boostorg/scope/blob/develop/include/boost...
Only for those willing to stomach the entire boost hairball, which in embedded environments might be too much to ask.
C: Simple Defer, Ready to Use
101–110 of 157 posts
Re: C: Simple Defer, Ready to Use
#102Does C really need this? Do languages need to grow in this way? The overriding virtue of C is simplicity.
> Does C really need this? Yes. > Do languages need to grow in this way? Yes. > The overriding virtue of C is simplicity. C is not simple. It used to be, but it's not been for a long time.
Re: C: Simple Defer, Ready to Use
#103For parity, C should add a `prefer` keyword that hoists statements to the top of the function.
Re: C: Simple Defer, Ready to Use
#104I don't think "you can implement this slightly modified version of the proposal in GCC macros" is a good reason to change the defer proposal. It doesn't work in clang¹ or any other non-GCC C implementation, a trailing semicolon after the close brace is an ugly wart, and saying "if you want to implement this all you have to do is implement [[gnu::cleanup]] and nested local functions" is presumably much more work for o…
Re: C: Simple Defer, Ready to Use
#105The function signature is `void(MyType*)` if it is used like:
__attribute__((cleanup(MyTypeCleanupFunction)))
MyType myType;
If it's a pointer, it'll call the cleanup as a double ptr, and etc.
Note that the specified cleanup function can do anything you want to, though the intended purpose is to cleanup the variable when it goes out of scope.Re: C: Simple Defer, Ready to Use
#106One can use C's `cleanup` attribute like a defer. https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attribute... The function signature is `void(MyType*)` if it is used like: __attribute__((cleanup(MyTypeCleanupFunction))) MyType myType; If it's a pointer, it'll call the cleanup as a double ptr, and etc. Note that the specified cleanup function can do anything you want to, though the intended purpose is to cleanup t…
Re: C: Simple Defer, Ready to Use
#107Re: C: Simple Defer, Ready to Use
#108Earlier quoted context omitted.
I use a few strategies instead of goto: (1) For simpler cases, wrap in do {} while (0) and break from the loop (2) For multiple cleanups, use same technique combined with checks to see if the cleanup is required. E.g. if (f != null) fclose (f) (3) put the rest of the stuff in another function so that the exit code must run on the way out. In 35 years of coding C/C++, I've literally never resorted to goto. While conve…
35 years of coding C don't amount to much if you ignore best practices. Look at the Linux kernel to learn how gotos can be used to make the code safer and improve clarity. There's probably something wrong if a substantial project in C does NOT use gotos.
Re: C: Simple Defer, Ready to Use
#109Very nice, using the goto trick to perform cleanups always felt dirty to me.
I use a few strategies instead of goto: (1) For simpler cases, wrap in do {} while (0) and break from the loop (2) For multiple cleanups, use same technique combined with checks to see if the cleanup is required. E.g. if (f != null) fclose (f) (3) put the rest of the stuff in another function so that the exit code must run on the way out. In 35 years of coding C/C++, I've literally never resorted to goto. While conve…
for (…) {
for (…) {
if (…) {
…
goto found;
}
}
}
found:
This is straightforward with goto and may even be vectorizable. I guess you could move the loop to a separate function or add additional flags to each loop, but neither of these seems like an improvement.Re: C: Simple Defer, Ready to Use
#110Earlier quoted context omitted.
Dijkstra was not wrong. Modern programmers are wrong in thinking that the goto that they use is what Dijkstra was talking about, merely because of the fact it happens to be called the same thing. I mean, I get how that can happen, no sarcasm, but the goto Dijkstra was talking about and what is in a modern language is not the same. https://jerf.org/iri/post/2024/goto/ The goto Dijkstra is talking about is dead. It liv…
Your analysis is wrong. Dijkstra was a big proponent of structured programming, and the fundamental thesis of his argument is that the regular control flow structures we're used to--if statements, loops, etc.--all represent a tree-based data structure. In essence, the core argument is that structured programming allows you to mentally replace large blocks of code with black boxes whose exact meanings may not be impor…
The modern goto is not the one he wrote about. It is tamed and fits into the structured programming paradigm. Thus, ranting about goto as if it is still the 1960s is a pointless waste of time. Moreover, even if it does let you occasionally violate structured programming in the highly restricted function... so what? Wrecking one function is no big deal, and generally used when it is desirable that a given function not be structured programming. Structured programming, as nice as it is, is not the only useful paradigm. In particular state machines and goto go together very nicely, where the "state machine" provides the binding paradigm for the function rather than structured programming. It is perhaps arguably the distinctive use case that it lives on for in modern languages.