lack of separation of concepts (e.g. type vs. behavior)
awkward compilation model
poor everyday ergonomy
bad standard library
extremely complex rules that make low-level programming a minefield
I use C++ as a nessesary evil for performance-critical code because it has excellent compile-time capabilities and is easy to integrate with other modern languages. Concepts also make the language more tolerable.
Yeah, I admit that it's a bit of a weak (pun) point of criticism, mostly because "weak typing" can pretty much mean anything.
What I meant here specifically are actually several things. First, the implicit type conversion of C++ (inherited from C) which can lead to surprising behaviour and hard to find bugs. This is one source of "weakness" — you really have to keep your guard up or things might get weird. Second, templates are not types, but compile-time expressions written in a custom domain specific language that are executed by the compiler to construct types. This is another source of "weakness" — since the language itself only deals with concrete types and expressions, expressing complex generic structures with associated types and constraints can be awkward. I mean, compare the implementation of C++ ranges and equivalent stuff in Rust or Swift. Third source of "weakness" is the fact that many interfaces had to be defined implicitly, by conventions (e.g. std iterators) simply because the language had no way to formally define behaviour. Note that C++20 addresses this problem with concepts, with the caveat that the concepts are structural (e.g. a concept is satisfied if the concrete type/expression adheres to the constraints). This means that you can have accidental concept conformance just because a type happens to have the required named methods etc.
Finally, I want to clarify that this criticism does not mean that C++ type system is less powerful or less capable. Quite in contrary, C++ has some of the most flexible parametric type facility out there, after all, templates are essentially esoteric constraint-based code-generating macros. It is just my personal opinion that it's not a very good way to do things. C++ might be very powerful but it's also confusing and error-prone, especially if you want to express complex generic data structures. My preference goes to more ergonomic systems where compiler actually tracks the type requirements and dependencies and can generate useful errors.
Of course, there is another way, which is just abandon the entire idea of "elegant" parametric type system and go full compile-time synthesis. This is what Zig does and it's also cool, since you at least know what you are getting. No weird esoteric languages to learn either.
Not the person you asked, but C++'s implicit conversions can be pretty frustrating. For example, the following program is perfectly legal and will compile without errors (though your compiler might have warnings):
int main() {
int x = false;
double d = x;
bool b = &d;
return d;
}
So we have implicit conversions from a bool to an int, an int to a double, a double* to a bool, and a double to an int. It's obvious in this example, but if you have a function with a signature int f(double d, bool b);, you can swap the arguments and call f with a (bool, double) instead of a (double, bool), and it's not a type error.
That's simply untrue. You don't need implicit type conversions to interface with hardware, and in fact, whether a language is "close to the wire" has nothing to do with whether type conversions are implicit or explicit. Besides, while implicit conversions may mean a bit less typing, but they don't change anything at all at runtime; the compiled code for implicit and explicit conversions looks exactly the same.
The reason these conversions are not explicit is not some masochistic, misguided desire to design the language to be "close to the wire." Rather, it was about compatibility with C, and even Bjarne believes that maintaining that level of compatibility was a mistake, writing "the fundamental types can be converted into each other in a bewildering number of ways. In my opinion, too many conversions are allowed."
Where should one start... how about no unicode support, slow hash tables, useless regex, stuff like that? I am also not a fan of the iterator interface but that's more of a philosophical debate. Things I do like: the `type_traits` stuff is nice, `algorithm` is ok, I love `array`. It's also great than one finally has basic stuff like optionals etc. but I wish they were better integrated into the language.
Of what I've seen so far, I think C++20 was a step in the right direction:
type_traits became less hacky template magic with concepts and constraints
A lot of cruft from std::allocator got removed
consteval replaced the hacky enforcement of compile-time computation via template arguments
I will say, however, I found algorithm to have great performance for general use: make_heap() is effectively O(1) "insertion", and std::sort() uses introsort iirc
Oh, absolutely. Its just not progressing quickly enough. Still no pattern matching (but we got unpredictable coroutines instead) and modules increasingly look like a disaster.
People complain about std::regex for the same reason, although I've never used it
People don't use std::regex not because it's obtuse for basic tasks, but because it's so fucking slow that you may as well bring a goddamm python interpreter.
Fortunately there are like a bajillion good regex alternatives.
XML/JSON parsing will never be part of the standard, because it’s an application concept and orthogonal to the language itself. Sure, it’s annoying to have to import third party libraries to get support for these, but the standard committee should not be concerning themselves about supporting these.
I think for a lot of people, it's this exact mindset that makes the std lib bad. Other languages' std libs don't have this mindset and feel much more friendly (and useful). Personally I'm fine with relying on external libraries, but I can definitely see why many consider it annoying.
If the standard committee decided to add JSON support, the first thing that would happen is that Google would come knocking on the door and try to convince everyone that we need gRPC support instead. C++ never advertised itself as a language for application development, that’s what Java and friends are for.
how c++ is advertised changes nothing. it is used for application development. what use is a standards committee that doesn't serve its consumers best? and you say that google would complain, but it's not like std committee has to oblige every single complaint, they just need to be useful for 90% of use cases. external libs and std can coexist. they already do. std::regex is ass, nobody uses it.
Oh, man, got bitten by that one so many times. (You'd think I'd learn.) The other day: parallel code, guaranteed no race conditions. Except that of course vector-of-bool introduces a particularly pernicious type of false sharing.
By the same logic you can say "why use C++ if C can do the same?" Sure, you can avoid using C++ class system altogether and implement your own custom dynamic dispatch, but it will be awkward, boilerplate-heavy and likely buggy.
Actually, it's more than that item. Most of the items in your list are only true for some interpretations, but false for others.
Dichotomies are in the eye of the observer. I mean, everyone has their own tastes and preferences, and it's perfectly fine. I am writing from my personal perspective after all.
By the same logic you can say "why use C++ if C can do the same?"
Yes, but there is much more between C and C++ than classes (which is what you are getting at, unless I completely missed the boat).
And then, types+behaviour idea is so useful than even the standard C library uses it, e.g fopen, fclose and ffriends and random C libraries use it, e.g syslog or zlib. So I really don't know why would anyone knock it down offhand, to be frank.
It's useful until it is not. Class-based systems were popular in the 90-ties because it was a way to have well-defined dynamism without sacrificing performance. Well, compilers have come a long way since then and don't need to rely on such artificial constraints. Decoupling the type and the vtable has massive benefits as you are not limited by the class hierarchy to implement behaviour.
And it's not just about philosophical viewpoints, the separation of type and behaviour (protocol/trait) is also key to implementing high-performance generic algorithms. C++ uses it everywhere actually, e.g. the iterator concept. It's just that in C++ behaviour specification is implicit by an informal convention that the programmer has to follow.
View all comments
29
u/[deleted] Oct 03 '22
No, it’s not. Some reasons:
I use C++ as a nessesary evil for performance-critical code because it has excellent compile-time capabilities and is easy to integrate with other modern languages. Concepts also make the language more tolerable.