> Kaboom. Oops. You can use auto, or you can use {} to make a vector, but you can't do both at the same time!
IMO it would be more confusing if that created a vector<int>. Unless the compiler is reading your mind, how is it supposed to know you meant a vector<int>, a set<int>, deque<int>, a struct { int a,b,c,d,e,f}, or some other object who's constructor can take 6 ints?
v := []TYPEHERE{1, 2, 3}
where TYPEHERE is the type of array[1] that you're creating. Is there any equivalent C++ technique here? Explaining all the machinery may blow out a novice's mind, but I think explaining "You need to give C++ some clue about the type" isn't so bad.[1]: Before you complain I just created a slice, remember the slice has to point at an array; I've created both a slice and an array so it's not "wrong" to say I've created an array with that syntax.
TYPENAME v {1, 2, 3};
TYPENAME v = {1, 2, 3};
auto v = TYPENAME {1, 2, 3}; x = [1, 2, 3, 5, 8, 13] # this is a list of integers
y = set([1, 2, 3, 5, 8, 13]) # this is a set of integers
There's nothing inherently wrong with C++'s initializer list approach. Nor is there anything inherently wrong with auto. But they collide in unfortunate ways because they haven't been designed such that they go well together. C++ is full of things like this. Responding with "well, that makes the most sense" for any given feature is missing the point entirely.> x = [1, 2, 3, 5, 8, 13] # this is a list of integers
> y = set([1, 2, 3, 5, 8, 13]) # this is a set of integers
Actually, that's not the best way to do it:
y = {1, 2, 3, 5, 8, 13} # this is a set of integers without creating a list first.
z = set(1, 2, 3, 5, 8, 13) # so is this
In C++ {} is syntactic sugar for "Create an initializer_list with these values," which is what happens when it's assigned to an "auto" variable. Not surprising at all. If you want a different type, you need to specify the type that you want.Maybe it's confusing to beginners and people who don't take the time to learn the language, but C++ isn't catering to those people.
Don't get me wrong, there are a lot of "gotchas" in C++, I just don't think the two you've posted are very bad.
I have problems with using C++ to build real projects with experienced programmers too, but those are mostly different problems.
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.
Maybe you don't like that, which is fine. But C++ is much broader than what's available in the standard library. C++ is a close second to C in the "how optional is the standard library?" contest.
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.
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?
>> 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};
The auto example creates an std::initializer_list which then can be converted into a vector or a set
std::vector<int> newvectorfromauto(v);
std::set<int> newsetfromauto(v);
Which seems the best you can expect with auto deduction. What else do you have in mind?Apparently, the first `+` is a problem, so just take it away:
return "mailto" ":" + name+'@'+domain; // there, problem solved. return "mailto"s + ":"s + name + "@"s + domain;
and it would compile fine. (s being a literal for std::string)
The article demonstrates string concatenation using +. That is great! Except it's inconsistent. The article's example works fine, of course:
I'll skip over the weird use of '' instead of "". Now let's say I want to prepend mailto: as well: So far so good. But I think it might be clearer if the colon was separated out: 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:
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! 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.