5 ms·
A major problem with C++ is a lack of consistency, which you run into if you start trying to generalize the ideas presented in this article. The article demons
by mikeash 12y ago
A major problem with C++ is a lack of consistency, which you run into if you start trying to generalize the ideas presented in this article.
The article demonstrates string concatenation using +. That is great! Except it's inconsistent. The article's example works fine, of course:
return name+'@'+domain;
I'll skip over the weird use of '' instead of "". Now let's say I want to prepend mailto: as well:
return "mailto:" + return name+'@'+domain;
So far so good. But I think it might be clearer if the colon was separated out:
return "mailto" + ":" + name+'@'+domain;
Oops, this no longer compiles. Hey beginner programmers, let's take time out from the arduous task of learning basic programming to understand what a const char * is and how it differs from const string and why you can + two strings or a string and a const char * or a const char * and a string but you can't + two const char *s.
The next example demonstrates initializing a vector:
vector<int> v = {1,2,3,5,8,13};
This is great, of course. The example after that introduces "auto" so you don't have to write the type of a variable. Well heck, let's combine the two!
auto v = {1,2,3,5,8,13};
Kaboom. Oops. You can use auto, or you can use {} to make a vector, but you can't do both at the same time! OK, beginners, let's take some more time out from learning what you came for and instead learn about the complex machinery that handles initializing a custom type with {} and why "a = b" can do arbitrarily complex things depending on the types of a and b.
I had a job in college tutoring students in my CS department's first-semester programming course, which was taught in C++. People would routinely come in with code that wouldn't compile because of some tiny mistake, but which produced literally pages of error output. There was no way for new students who were still struggling with the concept of a loop to figure out what they were doing wrong, besides finding somebody who had already been through it.
This was a long time ago, and C++ has improved, especially in the error message department. But these problems are still there, even if somewhat diminished, and other languages don't have them.
The fundamental problem with C++ is that it grew organically from humble beginnings without any apparent plan. This allowed to adopt a lot of nifty features and become extremely powerful, but it also means that there are a ton of bizarre corner cases and inconsistencies to deal with. For many projects, the tradeoff is worthwhile. But it's one of the worst choices imaginable for teaching new people to program.
- humanrebar 12y agoTo be fair everything you're upset about is a problem with the standard C++ library, not the language itself. Not to say that the language doesn't have issues. The initializer list example you provide is half of one, but only because people don't like to write algorithm-based code. This is actually fine: auto v = {1, 2, 3, 5, 8, 13}; for (auto i : v) { std::cout << i << '\n'; } ...all that being said, the C++ standard library gets strings very wrong. I've ranted about it here before. But, to be fair: 1. What language gets strings right? 2. C++ has some interesting ideas around the bend (concepts) that should make using library code much easier. Whether writing library code will be easier is another question, but I'm hopeful.
- mikeash 12y agoThe standard library is part of the language. But that aside, the fact that + doesn't work on string literals cannot possibly be a problem with the standard library. The fact that the language lets you extend + to work with some types but not others is not a problem with the standard library. As for initializer lists, the problem isn't simply that {} doesn't produce a vector, it's that identical-looking constructs do completely different things in different contexts. a = b can do completely different things depending on where you see it and what declarations are visible and on and on. Again, not a problem with the standard library. The standard library suffers from these problems, as it must since it exists within the language that causes them. But the problems are not in the standard library.
- claudius 12y ago> a = b can do completely different things depending on where you see it and what declarations are visible and on and on. I don’t see your problem with this. "a + b" certainly can and should do "completely different things depending on where you see it" and "+" is commonly used in completely different contexts in real-world usage, too, e.g. integer addition, integer addition modulo x, real number addition, complex number addition, vector addition, matrix addition, vector space addition, set addition/union etc. etc. To restrict "+" to only act on integers (or reals?) and require a +_real, +_complex, +_vector, +_matrix etc. seems unreasonable. Now, given that "+" may well do whatever it seems fit with its two operands, why should "=" not be allowed to do the same?