4 ms·
RAII
by raluk 2y ago
RAII
- f1shy 2y agoIs RAII Object orientation? I thought it was an idiom of C++ by Stroustrup.
- runevault 2y agoIt doesn't necessarily have to be OO no. Rust uses RAII and it uses traits instead of traditional OO style inheritance etc. You do need something like destructors/drop trait for it to work as far as I know though.
- tjoff 2y agoThe killer feature of RAII is when combined with exceptions. But sneaking in exceptions in an embedded C project isn't something I'd encourage or recommend. C++ imo doesn't offer anything compelling for the embedded usecase. Especially not considering all the footguns and politics it brings. You can of course be strict and diligent about it but if you are you are pretty much just writing C anyway. Better to do it explicitly. Allowing the use of the C++ standard library has been one of my biggest regrets (not that it was my decision to make, I fought it).
- AlotOfReading 2y agoC++ offers lots of compelling things for embedded use cases, like enum classes (finally fixed in C23), constexpr, std::optional, namespaces, and atomics/generics that are much smaller dumpster fires. There's an effort to extract the good parts and make it work for embedded use cases or even bring them into C. Khalil Estelle on WG21 has been working on an experimental, deterministic runtime for exception handling, to give one example. Constexpr is an example of the latter that's now showing up in C23.
- tjoff 2y agoI don't disagree, but these are in the ~2% convenience at most. With the huge baggage of including C++ in a project. The cost of learning C++ easily outweighs all those benefits. If you happen to have a proficient C++ team (that actually know embedded), go for it!
- AlotOfReading 2y agoSpeaking more broadly than just the std implementation, but result types like optional shouldn't be a 2% convenience, they should be used in most function calls that return errors. Rust is the obvious example here.
- tjoff 2y agoIf you argue for Rust I'm all for it, arguably much less of a learning curve than C++ too.
- Ragnarork 2y agoStrong disagree on that one. Even though C++ has a lot of features that take a while to learn, getting started with C++ is simpler by miles than getting started with Rust. One exception to that in my experience: dependencies, although I think that's a bit deceiving as yes, it's easier to get dependencies in Rust but in some areas they're way less mature and can sometimes be a pain to work with (usually when dealing with big C or C++ libraries that have been quickly glued to a Rust interface to be available in Rust).
- tjoff 2y agoAgree, writing bad C++ is easy. But being competent in C++ requires much more than being competent in rust. And not being competent in C++ is not great. You are going to be much more productive in C. The feedback loop is much faster, that is, feedback from your designs. Contrast with Rust which is harder to get going but doesn't require nearly as much to be decent.
- kccqzy 2y agoThere are a lot of large C++ shops that purposefully disable exceptions and yet still use RAII usefully. It's so useful that in many C codebases you see people using RAII. For example Gtk has g_autoptr and g_autofree. One of the most compelling things C++ offers to embedded use case is moving runtime initialization to compile-time initialization by liberally using constexpr functions. You literally ask the compiler to do work that would otherwise be done at runtime.
- tjoff 2y agoRAII is useful without exceptions yes. I guess it is the other way around. Exceptions are not useful without RAII (sorry not sorry most garbage collected languages ;)). But without exceptions it is mostly syntactic sugar anyway. If compile time initialization is the most compelling usecase I'll rest my case. Good feature, yes! Hardly worth switching language for.
- int_19h 2y agoIt's very useful syntactic sugar, though.