Re:C++ patterns using plain C
by torrance on Wednesday June 25, 2014 @05:26AM
(#7942942)
I couldn't care less about trying to reimplement classes in C — structs and functions will do just fine thank you very much. What about something actually useful, like RAII to simplify memory management?
[ Parent
| on HN ]
RAII
Please, yes. It happens I just spent the morning writing some C with files, views of those files etc, and I kept on whishing there was RAII, instead of having to build flow around the fact each of the operations might fail.
Is there a clean way to implement something like this in C?
I couldn't care less about trying to reimplement classes
indeed - the premise the OP starts with, i.e. 'here's the simplest class: BANG two subclasses' is also wrong. That is inheritance, not just 'the simplest class' as that wouldn't have anything to do with inheritcance.
Re:C++ patterns using plain C
by invernomut0 on Wednesday June 25, 2014 @07:33AM
(#7943310)
I remember something implemented as GCC extension (the cleanup variable attribute) but I never used it so I can't judge if it's really what are you searching for.
Re:C++ patterns using plain C
by guillaumec on Wednesday June 25, 2014 @09:58AM
(#7943866)
Gcc cleanup extension works very well for RAII, I used it extensively to write a simple profiler for a video game project (just add 'PROFILE' on top of a function to have it automatically profiled). The only problem is that this is not supported by MSVC.
It is supported by clang though. Maybe you could use clang-cl to get it into msvc no?
> Is there a clean way to implement something like this in C?
__attribute__((cleanup(…)))I really like talloc as a means of simplifying memory management. It's not, RAII, though.
Re:C++ patterns using plain C
by thinkpad20 on Wednesday June 25, 2014 @06:46PM
(#7947193)
Perhaps you might be interested in Rust?