News for hackers. Stuff that matters.
Re:Popular Myths about C++, Part 1 by mikeash on Tuesday December 09, 2014 @09:34AM (#8722890)
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.

Re:Popular Myths about C++, Part 1 by jlarocco on Tuesday December 09, 2014 @10:08AM (#8723123)
> 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!

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?

Re:Popular Myths about C++, Part 1 by jerf on Tuesday December 09, 2014 @10:57AM (#8723428)
Go would do:

    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.

Re:Popular Myths about C++, Part 1 by TheCoelacanth on Tuesday December 09, 2014 @11:25AM (#8723627)
Yes, C++ has no shortage of ways to initialize variables. Any of these should work

    TYPENAME v {1, 2, 3};
    TYPENAME v = {1, 2, 3};
    auto v = TYPENAME {1, 2, 3};
Re:Popular Myths about C++, Part 1 by mikeash on Tuesday December 09, 2014 @10:13AM (#8723156)
The way most other languages handle it is that the list literal construct just creates a list, and if you want something else then you need to explicitly convert. For example, in Python:

    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.
Re:Popular Myths about C++, Part 1 by jlarocco on Tuesday December 09, 2014 @10:32AM (#8723264)
> The way most other languages handle it is that the list literal construct just creates a list, and if you want something else then you need to explicitly convert. > For example, in Python:

> 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.

Re:Popular Myths about C++, Part 1 by mikeash on Tuesday December 09, 2014 @10:46AM (#8723351)
Um, the whole point of this discussion is about the claim that C++ is a good language for beginners to learn.

I have problems with using C++ to build real projects with experienced programmers too, but those are mostly different problems.

Re:Popular Myths about C++, Part 1 by maxlybbert on Tuesday December 09, 2014 @10:50AM (#8723381)
Well, to be fair, with "auto x = { 1, 2, 3, 4, 5, 6}" you will get a variable named "x", but it won't be a std::vector<int> (it'll be a std::initializerList<int>).
Re:Popular Myths about C++, Part 1 by humanrebar on Tuesday December 09, 2014 @09:46AM (#8722959)
To 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.

Re:Popular Myths about C++, Part 1 by Retra on Tuesday December 09, 2014 @12:01PM (#8723899)
Standard libraries are part of the language. I would go so far as to say anybody who designs a language that isn't meant to make their standard libraries powerful, consistent, and elegant isn't doing any favors for the discipline.
Re:Popular Myths about C++, Part 1 by humanrebar on Tuesday December 09, 2014 @03:53PM (#8725643)
C++ is a bit different in the variety of domains that it's applied to. To expect the same standard library to be useful in games, microcontrollers, medical instruments, aircraft control systems, kernels, and CRUD apps is a huge ask.

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.

Re:Popular Myths about C++, Part 1 by mikeash on Tuesday December 09, 2014 @09:55AM (#8723028)
The 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.

Re:Popular Myths about C++, Part 1 by claudius on Tuesday December 09, 2014 @10:14AM (#8723165)
> 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?

Re:Popular Myths about C++, Part 1 by npalli on Tuesday December 09, 2014 @11:14AM (#8723540)
>>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};

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?
Re:Popular Myths about C++, Part 1 by netheril96 on Tuesday December 09, 2014 @10:18PM (#8727497)
What he has in mind is that it is logical and reasonable for C++ to behave in this way, but completely incomprehensible for beginners who has just learned the basics of programming.
Re:Popular Myths about C++, Part 1 by Scea91 on Tuesday December 09, 2014 @10:50AM (#8723384)
The aim of C++ is not to be accessible to beginners. There are other languages for that. The philosophy of C++ is to give you power and it's up to you how to handle it.
Re:Popular Myths about C++, Part 1 by mikeash on Tuesday December 09, 2014 @11:00AM (#8723448)
The article explicitly states that C++ is a good language for beginners, and the entire point of my comment is to argue against that.
Re:Popular Myths about C++, Part 1 by maratc on Wednesday December 10, 2014 @06:53AM (#8728723)
> return "mailto" + ":" + name+'@'+domain;

Apparently, the first `+` is a problem, so just take it away:

    return "mailto"  ":" + name+'@'+domain; // there, problem solved.
Re:Popular Myths about C++, Part 1 by mikeash on Wednesday December 10, 2014 @02:22PM (#8731098)
Of course. Now, do you want to sit down and explain why that's necessary to somebody who's still grappling with the concept of for loops?
Re:Popular Myths about C++, Part 1 by maratc on Thursday December 11, 2014 @12:02PM (#8735787)
That was supposed to be a sarcastic joke about yet another obscure C++ perk. I guess it wasn't very funny.
Re:Popular Myths about C++, Part 1 by axiak on Tuesday December 09, 2014 @09:04PM (#8727280)
Not that this is necessarily any cleaner, but in C++14 one would write:

    return "mailto"s + ":"s + name + "@"s + domain;
and it would compile fine. (s being a literal for std::string)
Re:Popular Myths about C++, Part 1 by mikeash on Wednesday December 10, 2014 @02:23PM (#8731106)
It's sad that it can't become the default for "" literals without breaking a ton of code, but I'm glad to see this change.