| > the terribleness of exceptions is way overhyped It's not that exceptions are terrible. I have nothing against them. The thing is that most of the time they are not affordable, especially in embedded. Some compilers don't even support them (some 8-bit IIRC). Most (including mine) embedded C++ programs have exceptions disabled. > return a `std::optional` The same goes for std. I don't know the overhead of possibly duplicating these classes(1) for every instance of std::optional. Most (including mine) embedded C++ programs don't use std. 30KB of extra code is nothing for desktop/server applications, but it's not convenient for a MCU with 64KB of flash. (1): https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-... > just use a factory function instead This is practical with an efficient heap allocator (which I might not have). What happens if I want a scoped (on stack) instance of a class? See how things are quickly getting more complicated with C++? |
> This is practical with an efficient heap allocator (which I might not have). What happens if I want a scoped (on stack) instance of a class?
There is absolutely no reason to do a heap allocation for a factory function in modern C++ (or even relatively ancient C++, in fact). The fact that you would think so indicates you simply don't know C++ at all.
Tell me, where in these six lines do you see a heap allocation? Where in the total of 6 instructions this compiles down to do you see anything that couldn't run in any embedded environment? Tell me how these six instructions wouldn't fit inside your 64kb of flash:
https://godbolt.org/z/rErWPbbbx
And again, EVEN IF you're so religiously and irrationally opposed to using std::optional, you can just return a default-constructed object and an extra bool indicating successful construction. I don't know why you would considering you could just return an optional, but whatever, you can do it that way if that's what you prefer.
You're just wrong about this stuff, and it's this kind of lazy, uniformed criticism of C++ that really rubs me the wrong way. If you wanna use C, use C! Nobody's stopping you, it's a fine language. Just leave the C++ discussions to the people who actually know what the language is.