by Aardwolf · 8y ago · 0 comments
That would allow cleanup and other such actions without resorting to such hacks as: "goto cleanup" pattern, separate functions, fake for loop to break out of, ...
It would be nice to have this be a part of the standard.
You can do it in the 20-year-old version of it, and draft versions many years before that.
Edit: Ah, I see; you're more worried about them turning C into the next C++ :) I might even agree some days.
/**
* GCC and Clang extension to call cleanup function at exit
* from the function with pointer to given function as argument.
*/
#define defer(func) __attribute__((cleanup(func)))
#ifdef _CRUST_TESTS
// Sample destructor, to test defer
static void _tst_charp_destroy(char * * value) {
if(*value) {
free(*value);
*value = NULL;
}
}
// Test case
it(defer, "must deallocate object at the end of the function") {
defer(_tst_charp_destroy) char * s = calloc(1, sizeof(char));
(void)s;
}
#endifD has scope(exit){...} which is similar to constructs like defer {...}
Many things can be done in some form with macros and the C11 generic keyword if they have to be, but defering statements to run on scope exit is still elusive as something that can be relied upon.
Many programmers like myself have no problem with it whatsoever.
Change in a key infrastructural component like this is only destabilizing.
The ISO C people should have the decency to disband and work on their pet language research projects in private.
The issue with cleanup on scope exit is that this introduces ambiguous code-flows. You would need to demonstrate a strong advantage, beyond making life easier for the writer of the code.
EDIT: Sorry for the confusion, I misread the article and thought this was about C++2x.
If they would add that feature, you could use macros to get constructor and destructor like behavior (as long as it's within the same scope, you could write a macro that constructs your object, and in addition contains something like "at scope exit: run this function"), which would be super useful and as far as I'm aware not very complex for the compiler (as all they have to do is run some code whenever exiting scope) nor very far outside of the spirit of C (it's still very low level, just a small convenience feature with interesting implications, especially when used with macros).
It is the result of starting with C, and then accommodating a massive, sustained sequence of feature requests, exactly like ones we are seeing in this thread.
Everyone thinks that if just their pet feature request is added to C and everyone else's ideas are rejected, it will still be C.
This idea doesn't seem to be in that spirit to me. It's not trying to do something radical like, say, adding OOP semantics to C. It's suggesting that C, a structured programming language, get a feature designed to improve the ergonomics for people doing structured programming in C.
But then you also want templates of course to get rid of your void*. And oh, wait, what's that? A cross-platform thread implementation (well, sort of, depends on platform)? A string thing which I can search without strstr? Anonymous functions which capture? Auto? Gimme gimme :]
C++ is not a strict superset of C. You actually have to cast void pointers, for example.
> But then you also want templates of course to get rid of your void*.
No, actually, I think I can live without that, but thank you.
> And oh, wait, what's that? A cross-platform thread implementation (well, sort of, depends on platform)?
C11, threads.h. Been there, got that.
> A string thing which I can search without strstr?
Okay, I'll give you that, string handling is hell.
> Anonymous functions which capture?
Have their uses, but I'm not sure if C is really the right place for it.
> Auto?
auto (as used in C++11 and later) is a solution in need of a problem in C. When you don't have piles of templates and iterators, you also tend not to need to automatically derive types. In a world where signed/unsigned integer comparison can have fatal consequences, you probably do want to be very sure about your types.
https://stackoverflow.com/questions/2053029/how-exactly-does...
But who are we to ask..? Just programmers, i.e. mere mortals nobody really cares about. :-)
https://www.boost.org/doc/libs/release/libs/scope_exit/doc/h...
// WTFPL
class Finally {
std::function<void()> f;
public:
Finally(std::function<void()> f) : f{f} {}
~Finally() {f();}
void disable() { f = [](){}; }
};
Which looks much prettier: socket* s = open_socket(); // or whatever
Finally cleanup_socket{[](){ close_socket(s);}};
// optionally, to keep it alive:
cleanup_socket.disable();
Am typing on mobile. No guarantees about syntax errors. I still need to look into where the right places to put r value references and/or std::move are, but for the most part, I never put anything other than a pointer in the closure so I'm not worried about copy constructor cost. auto cleanup_socket = make_scope_guard([&]{ close_socket(s); });
Searching for scope_guard yields lots of alternative implementations.I disagree.
> to run code when leaving a scope, no matter how you leave
This, and various other features, were already added to C long ago. The resulting dialect was called C++.
Are you implying that people who want new features in C should just migrate to C++ because it has even more features? Or that C is perfect as-is and anyone who wants to add new features to it should bug off to another language? Or that this particular feature is somehow equivalent to (or a slippery slope toward) adding a whole set of OOP features to C, so we might as well take that to its logical conclusion?
A single, clearly articulated response would do a lot more than a fusillade of terse barbs to help others understand why you think this is a bad idea.