Also, while iostream is a bit of a mess, having it be strongly typed means that it's, in my opinion, well worth using over cstdio.
One thing I was surprised not to see mentioned here is multiple inheritance. In my experience it gets really messy really fast.
Also I really recommend reading the linked archived Boost discussion about a geometry library, it's quite funny. It starts with:
double distance(mypoint const& a, mypoint const& b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
And after a couple of pages of refinements ends up with: template <typename G1, typename G2>
double distance(G1 const& g1, G2 const& g2)
{
typedef typename strategy_distance
<
typename coordinate_system<G1>::type,
typename coordinate_system<G2>::type,
typename point_type<G1>::type,
typename point_type<G2>::type,
dimension<G1>::value
>::type strategy;
return dispatch::distance
<
typename tag<G1>::type,
typename tag<G2>::type,
G1, G2, strategy
>::apply(g1, g2, strategy());
}
But hey, it can compute distances in non-cartesian hyperspaces so that's pretty cool.There's nuance to banning that since implementating multiple interfaces is technically multiple inheritance in C++. So I think I'd agree with you but with an exception for pure-virtual classes.
> Also, while iostream is a bit of a mess, having it be strongly typed means that it's, in my opinion, well worth using over cstdio.
Orthodox C++ would ban this but you can have both printf-style with type safety with https://github.com/fmtlib/fmt
Which inspired C++20's std::format: https://en.cppreference.com/w/cpp/utility/format
Which is also banned by Orthodox C++
Orthodox C++ bans exceptions, so if an allocation failure occurs within one of those classes, the only thing your program can do is abort.
That’s fine for some programs - Google’s C++ style guide takes this approach, as does LLVM. But for other programs, where aborting on allocation failure is not acceptable, there’s no way to use those classes without using exceptions.
Note the style guide says they’d rather use exceptions but had too much legacy code (and now must have a couple of orders of magnitude of it by now): “On their face, the benefits of using exceptions outweigh the costs, especially in new projects. However, for existing code, the introduction of exceptions has implications on all dependent code. … Because most existing C++ code at Google is not prepared to deal with exceptions [don’t use them]”
Google’s non use of exceptions is often used as justification for not using them either, by people who haven’t actually read Google’s style guide.
Also: few of us have Google sized problems.
The comparison you need to make is instead exceptions vs. return values. Unless you're going to argue all errors should abort by default or similar.
I do wish noexcept had better support, or was even the default (C++ and wrong defaults, a tale as old as time). But it's disingenuous to compare exceptions against nothingness.
If you call a function within the condition of an if statement, the compiler needs to conservatively assume that the execution flow from there is either to the rest of the conditionsl or to either one of the catch blocks or the functions' stack unwinding. If there cannot be an exception, the execution flow has none of these additional branches that bog down the DFA.
Sure but that seems like the edge case not the general case? Nearly any desktop or mobile app or game won't have any such constraints, for example. And you've also got overcommit to deal with on those platforms, so it's not like your allocation failure will actually happen at malloc time either.
So outside of embedded, which tends to have a variety of constraints, what even pretends to handle allocation failures across the entire program?
I'm not entirely sure why you'd want a custom allocator for shared_ptr, though.
The deeper rationale is that some systems like to assign fixed memory pools to subsystems. These may be reset or destroyed at certain points in the applications life cycle and the general expectation is that this frees all memory used by said subsystem. If you still want to use the STL in that context, you need to provide custom allocators for everything that potentially allocates.
At least you can write a replacement for shared_ptr. The same is not true for std::function. This is the only named type a capturing lambda converts to according to the language standard and it's an STL type with complex behind the scenes behavior.
A capturing lambda is just a class with an operator(). It's complicated to do what std::function does, but fully possible.
In fact, custom std::function replacements have better lambda support than std::function itself. Such as unique_function in https://github.com/Naios/function2 which can handle non-copyable lambdas.
Don't embedded platforms mostly use something other than STL anyway?
In general, when discussing which language features to use, I would keep in mind that something can work well for an application that is fairly straightforward and has modest performance requirements, while being a complete no-go for a different use case. I'm not saying we should throw C++ in the bin, but I also think many people have their reasons for saying some C++ features are more trouble than they're worth.
[0]: https://stackoverflow.com/questions/42588264/why-is-stdunord...
(Edited to remove the bit about std::vector, as this argument does not apply to it.)
I feel that banning them altogether may lead people to implement stuff from scratch even when they don't need it, creating another possible source of bugs and vulnerabilities.
BTW, vector is contiguous, not continuous.
So optimal unordered map implementations, like Google's absl::flat_hash_map or Facebook's F14 would be similarly banned as they internally allocate.
I'd definitely recommend those libraries over std::unordered_map for sure, but it's not like std::unordered_map is unusably slow or broken, either. It's fairly comparably to Java's HashMap that everyone uses without thinking about it.
that's when you use one of the two hundred alternative implementations which keeps the same API but offer different performance compromises & tradeoffs :
https://martin.ankerl.com/2019/04/01/hashmap-benchmarks-02-0...
If you prototyped with std::unordered_map you'll likely just have to change a couple types and add the relevant includes here and there, rerun your benchmarks, and tada.
> Don't use anything from STL that allocates memory, unless you don't care about memory management.
is not meant condescending but literally. Ergo, if you don't care about memory management (which is fine), then feel free to use lots of STL stuff.
Case in point: for lots of embedded software, memory matters a lot and it's important that it's obvious to the reader of the code what memory gets allocated where. The STL classes you quote make this harder to see. But if you have lots of RAM anyway (ie "you don't care about memory management") then this does not matter much.
A key goal of this list appears to be, I quote, "Projects written in Orthodox C++ subset will be more acceptable by other C++ projects". This particular guideline makes it more likely for your code to be deemed acceptable by embedded programmers so it seems to fit. I don't think it was intended to be condescending in any way. I think the same holds for the other points.
It could've definitely been written a bit clearer though.
I don't see it that way. Plenty of people care about memory management and also see the STL as a fine tool to use. Little is gained from a memory management perspective by avoiding e.g. std::vector. Whether condescension is intended or not, this definitely comes across as talking down to me, and worse, it is incorrect.
If they didn't intend condescension, they should not have put a comic displaying exactly that (and with essentially no further relevance) into the middle of the article.
This isn't really true. All of these containers take custom allocators, which can be tailored to your use case (eg: a stack based allocator).
The important part is "if you don't care about memory management".
In language benchmarks, C++ is often portrayed as less efficient/slower than C. C++ is also commonly shunned when it comes to embedded software, especially on low performance chips. It doesn't have to be! C++ is (almost) a superset of C, and generally, they use the same compiler backend, so your C code should compile on a C++ compiler and generate the same binary.
Now that you know you can write C++ running as efficiently as C, you can start to carefully add features that will keep the spirit and efficiently of C while making your life easier, or even improve performance. For example you may want to replace macros with templates, use proper objects instead of doing like stdio does with FILE*, use namespaces, etc...
That's exactly what Orthodox C++ is about. It is C++ for those who want to write C. There is absolutely nothing wrong with STL containers and smart pointers and all the fancy stuff that make up modern C++, there is also nothing wrong with using languages that have heavy runtimes and garbage collectors, it is just not the use case Orthodox C++ is addressing.
I think that's the good thing about the mess that is C++11 and beyond. You can pick what you want. You can stay low level and know exactly the memory layout of your program. Or you can choose not to have a single raw pointer and let it manage the memory for you.
Note: There are still a good reasons to use C over C++. A big one is that linkage is a lot simpler and more compatible in C. C++ compilers do name mangling to support things like namespaces and polymorphism and require the linker to understand their particular conventions, and you may need the right libstdc++ for your target. You also need to be aware of static initialization.
Source? Even in the somewhat silly computer language benchmark game C++ is fairly consistently ranked faster than C: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
The concept isn't terrible necessarily, but the execution and condescension sure is. And some of the rules are just pointlessly argumentative. Such as refusing to use <cstdio> and instead using <stdio.h> - those are officially documented to be different things with different behaviors. Blanket banning one of them doesn't improve simplicity, especially if you're blanket banning the "wrong" set of headers. Unless the goal is to also ban namespaces, but that's not called out as such (and namespaces are one of the least contentions C++ features - everyone seems to like them well enough).
Or similarly:
> Don't use anything from STL that allocates memory, unless you don't care about memory management.
So don't use std::vector? Or std::unordered_map? Or std::string? These are all perfectly fine classes, banning them makes no sense at all. Maybe the goal was to ban "hidden cost" classes like std::function, but rolling your own 'C-style' is a hell of a lot more complex & error prone (raise your hand if you've seen a C pointer callback that forgot to have a void* context or a mismanagement of said void* context...)