5 ms·
Show HN: Automatic cleanup in C using with-statements
- NullPrefix 6y agoHey, it's pretty nice. Reminds of the old adage - You never code in plain C, you need to first #define some macros and then you can proceed with metaprogramming
- mycall 6y agoUnless you are doing embedded programming.
- asguy 6y agoEspecially when you are doing embedded programming. You think those GPIO registers are going to encode themselves?
- kettro 6y agoMacros are the lifeblood of embedded C. I have a use case where I need to read, shift, and OR a value, then write back. If I need to do that for more than 1 thing, sure as shit I'll write a macro.
- taeber 6y agoThank you! I'd much prefer to not have to use macros, but I tried to make the with/withif mimic what I wish was a real C statement.
- spacedcowboy 6y agoOr you could use attributes... https://www.reddit.com/r/embedded/comments/gt8vsc/using_raii_in_c_for_fun_and_profit/ https://www.reddit.com/r/embedded/comments/gt8vsc/using_raii...
- taeber 6y agoYes you can. I did mention GCC's cleanup attribute along with MSVC's try-finally construct. Another user was kind enough to even provide an example in their GitHub issue: https://github.com/taeber/cwith/issues/1 https://github.com/taeber/cwith/issues/1.