by pjmlp · 4y ago · 0 comments
More often than not, it is a matter of not wanting to use C++ than it not being available.
Yes, it can work for simple Arduino sketches, and I won't say that there are not complex embedded projects in C++.
But... why should I want to use C++?
For one, I use a small subset, like it's C with classes. I like function overloading and default argument values, initializing default values for some structs, not having to type typedef struct, the fact that for a time I could declare variables mid-function and using literal bool, true, false, etc.
But full-blown C++ on embedded (MCU)? I think I'll pass.
You would want to use C++ for stronger type safety, proper enumerations, templates instead of undebuggable macros, namespaces,....
https://news.ycombinator.com/item?id=31406942
"CppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17”"
https://www.youtube.com/watch?v=zBkNBP00wJE
MCUs also don't support full-blown C, and it isn't a show stopper to keep advocating it, so why should it be for a language with better safety options, even with constraints.
You can still use a subset of stdio and stdlib. Newlib also provides hooks you can use to implement fopen and the like, if you want.
> better safety options
Safety is not always the utmost priority. At least, not that kind of safety. It seems people forget this concept from time to time.
> templates instead of undebuggable macros
This is if you use complex macros, which is not always the case. I've been doing embedded for 20 years and I rarely (very rarely) find problems with macros.
But you know what's undebuggable on embedded? Complex inheritance, which sooner or later you'll hit with OOP.
For a system in which you don't have a screen, or logs, or any kind of output, that is usually not in your table, and you have to study failures by telling someone to look at an LED, or by guessing what could have gone wrong, you have to keep your system simple.
Add templates to the mix and good luck opening the project six months later, when a customer calls with a problem.
Edit: But again, used with caution, C++ can be used in embedded, at least the way I use it, as explained before.
Yeah, safety and IoT unfortunately aren't something that go together.
Using C++ doesn't require OOP all over the place, nor crazy template metaprogramming.
The same reason you give for using macros.
That's perfectly valid. I reach for C when I need to write fairly small programs. When C does not fill my needs anymore, I don't reach for C++. There are better high-level languages.
C++ is not a replacement for C. When one of the worlds foremost experts on C++ complains that the language is too complex to understand[1], passing on C++ is a reasonable position.
[1] https://steven.brokaw.org/posts/scott-meyers-cant-remember-c...
Try to do a pub quizz about a begginiers friendly language like Ruby or Python, that spans over all their versions.
By the way, the D language (which has been with us for a while) has long positioned itself as a viable alternative to C++.
It's a very restricted subset of C++ that has almost no advantages over plain C, and some number of disadvantages.
- constexpr instead of macros
- if constexpr instead of macros
- stronger type checking
- type inference
- strong type enumerations
- namespacing instead of pre-historic naming prefixes
- classes as means to enforce type invariants
I see some advantages.