7 ms·
Perfectly valid to do if you need to interface with a large C code base and you just want to do some simple OO here and there. Especially if you cannot have run
by com2kid 2y ago
Perfectly valid to do if you need to interface with a large C code base and you just want to do some simple OO here and there. Especially if you cannot have runtime exceptions and the like.
This is how I managed to sneak C++ into an embedded C codebase. We even created some templates for data structures that supported static allocation at compile time.
- f1shy 2y agoWhat would be an example of "simple OO here and there" that cannot be done cleanly in plain C?
- raluk 2y agoRAII
- 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).
- adamrezich 2y agoNamespaces, methods.
- f1shy 2y agoNamespaces is not object orientation, is it? Am I missing something? You can place functions (methods) inside of structs in C23, can't you?
- staunton 2y agoOn a high level, "object orientation" means you think of your code as representing the state and interactions of objects. You can equally well do this in assembly. If you think of some namespace as a "singleton object" then that's what it is. I guess what you're really asking is what are the best or most common ways to do OO in C?
- f1shy 2y agoOh. I learned that object orientation is primarily a way to structure data and code, such that the data is encapsulated with the code that works on it, in so called objects. So an Object is the Data, plus the functions that work on the data, an ensure that some invariants are kept. In OO parlance, that code gets executed by sending messages (calling methods). Where can I find something about objects being "think of your code as representing the state and interactions of objects" honesty totally new to me. So no, certainly I'm not asking ways to do OO in C. But it seems to be more definitions of object orientation as I thought...
- int_19h 2y agoThere's no clear definition of what OO is, so the best you can do pragmatically is look at mainstream languages that are broadly recognized as OO and try to deduce the commonalities. If you do that, you'll notice that, for example, encapsulation is not a part of that de facto definition, because languages like Python and (until recently) JavaScript lack it, despite being considered OO. Indeed, the only two things that appear to be consistently present in all OO languages are: 1) some notion of object identity as distinct from object state, and 2) runtime polymorphic dispatch.
- bobmcnamara 2y agoTemplating on pixel classes so that a blitter builds all supported pixel paths separately and inlines them. Yes you can do it less cleanly with macros or inline functions. But you can't do it performantly with struct and function pointers.
- deleted 2y ago[deleted]
- cozzyd 2y agoCRTP?
- com2kid 2y agoYou can do anything in C that you want to. Of course one can make v-tables and all of that, and even do inheritance. But having the "class" keyword is nice. Having built in support for member functions is nice. Sometimes a person just wants the simplicity of C++ 2003. (In reality I was working on a project where our compiler only supported C++ 2003 and we had a UI library written in C++ 2003 and honestly pure C UI libraries kind of suck compared to just sprinkling in a bit of C++ sugar.)
- throwaway2037 2y ago> You can do anything in C that you want to. How about destructors?
- com2kid 2y agoYou can obviously build any runtime system you desire in C, including one that parses and executes C code with additional features added in. The wisdom of doing this is questionable. Though I've actually seen macro systems that do things akin to destructors, although less automatically.
- nerfnet 2y agoSurely you are aware that C++ destructors are essentially made in C.
- pjmlp 2y agoYeah, but one should provide C++ type safe abstractions on top. Just like one doesn't use Typescript to keep writing plain old JavaScript, then why bother.