4 ms·
"Modern C" now effectively includes __attribute__((cleanup)), in as much as it's widely used by large free software projects and supported in both GCC and Clang
by rwmj 5y ago
"Modern C" now effectively includes __attribute__((cleanup)), in as much as it's widely used by large free software projects and supported in both GCC and Clang.
- teddyh 5y agoDocumentation: “cleanup (cleanup_function) The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored. If -fexceptions is enabled, then cleanup_function is run during the stack unwinding that happens during the processing of the exception. Note that the cleanup attribute does not allow the exception to be caught, only to perform an action. It is undefined what happens if cleanup_function does not return normally.” — https://gcc.gnu.org/onlinedocs/gcc-11.1.0/gcc/Common-Variable-Attributes.html#index-cleanup-variable-attribute https://gcc.gnu.org/onlinedocs/gcc-11.1.0/gcc/Common-Variabl...
- mrpippy 5y agoIs it widely used? I've only come across it in GLib (g_auto*), but even then it couldn't be used in GStreamer because they also support building with MSVC.
- fpoling 5y agosystemd code uses it widely.
- WalterBright 5y agoThis comes from the Scope Guard Statement in D, from an idea originally proposed by Andrei Alexandrescu and Petru Marginean. https://dlang.org/spec/statement.html#ScopeGuardStatement https://dlang.org/spec/statement.html#ScopeGuardStatement
- bsder 5y agoThat's just horrible. Why does the C committee keep wanting to make the language into C++? Is this something that rank and file programmers actually want? Is this something that library writers are demanding? At this point, I think I'm pretty fine with C anchoring down on features unless there is an absolutely pressing demand. There are enough newer languages experimenting in the space that I don't feel the need to pollute C with this stuff until those languages hash it all out.
- adrianN 5y agoRAII is one of the best things about C++. It makes cleanup much simpler than what you need to do without being able to bind cleanup code to scopes. You avoid the kind of complexity that can lead to "goto fail" style problems.
- bsder 5y agoIf you want RAII, then use C++--no one is stopping you. A lot of embedded developers STRONGLY disagree with you. There is a reason why embedded developers tend to turn off a whole bunch of C++ features. Note all the: "No RTTI. No templates. No exceptions." flags that embedded folks always engage. Now that VSCode is getting actual support for embedded (see all the work the Rust Embedded guys did, for example) as well as official ARM support (Eclipse Theia), various languages can be used for development. Thanks to this, people no longer need to force things because "If it's not in the C toolchain, nobody will support it." People don't need to torture C anymore to use their favorite language feature on their hardware of choice.
- adrianN 5y agoThanks, I did safety critical embedded development for while, in C++. We did turn off a bunch of features of course. We never heap-alloced for example. But destructors were used extensively to make sure that important actions happened during error conditions. We were lucky to be able to use C++ because we had a compiler for our target platform. Not every target has a C++ compiler, so I think adding something as useful as scope-bound cleanup to C makes perfect sense.