Comment by com2kid

17 hours 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.

What would be an example of "simple OO here and there" that cannot be done cleanly in plain C?

  • Templating 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.

  • You 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.)

  • RAII

    • The 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).

      6 replies →

Yeah, 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.