News for hackers. Stuff that matters.
Re:Practical Guide to Bare Metal C++ by kryptiskt on Tuesday March 10, 2026 @10:59AM (#47324178)
> Could turn it around as "everything you can do in C++ you can do in C with a lot less language complexity".

No, you can't, C is lacking a lot that C++ brings to the table. C++ has abstraction capabilities with generic programming and, dare I say it, OO that C has no substitute for. C++ has compile-time computation facilities that C has no substitute for.

Re:Practical Guide to Bare Metal C++ by myrmidon on Tuesday March 10, 2026 @12:08PM (#47325160)
My point is trivially true as far as computability goes, but that is not what I ment.

All those abstraction capabilities can be a big detriment to any project, because they always come with a cost, and runtime is far from the only concern.

Specifically in an embedded project, toolchain complications and memory use (both RAM and code) are potentially much bigger concerns than for Desktop applications, and your selection of programmers is more limited as well; might be much more feasible to lock your developers onto acceptable C coding standards than to make e.g. "template metaprogramming" a necessary prerequisite for your codebase and then having to teach your applicants electrical engineering.

Both object oriented programming and compile time computation is doable for a C codebase, just needs more boilerplate and maybe a code-generator step in your build, respectively. But that might well be an advantage, discouraging frivolous use of complexity that you don't actually need, and that introduces hidden costs (understanding, ease of change, compile time) elsewhere.

Re:Practical Guide to Bare Metal C++ by embeng4096 on Tuesday March 10, 2026 @11:50AM (#47324907)
Is there an example of the generic programming that you've found useful?

The extent of my experience has been being able to replace functions like convert_uint32_to_float and convert_uint32_to_int32 by using templates to something like convert_uint32<float>(input_value), and I didn't feel like I really got much value out of that.

My team has also been using CRTP for static polymorphism, but I also feel like I haven't gotten much more value out of having e.g. a Thread base class and a derived class from that that implements a task function versus just writing a task function and passing it xTaskCreate (FreeRTOS) or tx_thread_create (ThreadX).

Typed compile-time computation is nice, though, good point. constexpr and such versus untyped #define macros.

Re:Practical Guide to Bare Metal C++ by slaymaker1907 on Tuesday March 10, 2026 @12:26PM (#47325411)
std::array can sometimes give you the best of both worlds for stack allocation in that you statically constrain the stack allocation size (no alloca) while guaranteeing that your buffers are large enough for your data. You can also do a lot of powerful things with constexpr that are just not possible with arrays. It is very convenient for maintaining static mappings from enums to some other values.
Re:Practical Guide to Bare Metal C++ by csb6 on Tuesday March 10, 2026 @01:06PM (#47326014)
The generic algorithms that come with the C++ standard library are useful. Once you get used to using them you start to see that ad-hoc implementations of many of them get written repeatedly in most code. Since most of the algorithms work on plain arrays as well as more complex containers they are still useful in embedded environments.
Re:Practical Guide to Bare Metal C++ by CyberDildonics on Tuesday March 10, 2026 @03:02PM (#47327484)
You've never used a template for a data structure and you've never used a destructor to free memory?
Re:Practical Guide to Bare Metal C++ by rkagerer on Tuesday March 10, 2026 @10:18PM (#47331122)
I had been programming for a long time before I learned OOP. After some years playing with it, I came to the conclusion there's not much I can't do about as well using simple functions and structs. The key is a well thought out and organized codebase. Always felt polymorphism in particular seemed more trouble than it was worth.

I still use modern languages on a regular basis, but when I drop back to more basic languages there are only a few ergonomics that I truly miss (eg. generics).

Re:Practical Guide to Bare Metal C++ by jonathrg on Wednesday March 11, 2026 @12:04PM (#47337384)
Having a type safe generic ring buffer and such is nice
Re:Practical Guide to Bare Metal C++ by bigfishrunning on Tuesday March 10, 2026 @03:57PM (#47328046)
> C++ has compile-time computation facilities that C has no substitute for.

The substitute for this is that C is insanely easy to generate. Do your compile time computation in your build system.

OO is also pretty trivial in C -- the Linux kernel is a great example of a program in C this is very Object Oriented.