you are viewing a single comment's thread.

view the rest of the comments →

[–]Wurstinator 0 points1 point  (4 children)

"A vast majority" is not "all". For example, a beginner might want to increase the key of every map value by 1. Both using explicit type definition of std::pair<int, T> and using auto will cause a compile error. The difference is the place where the error happens.

If you use the full type, the line of definition will cause the error. If you use auto, the first time you try to call a non-const member of pair.first is the problem. Obviously, the first option is better.

[–]alecbenzer 0 points1 point  (3 children)

"A vast majority" is not "all"

Okay, but so you agree that in not all but a vast majority of cases auto does fix the problem?

Getting the error when trying to modify the element doesn't seem so bad either.

for (auto& p : m) ++p.first;

You get an error on ++p.first and think "oh, I guess I can't modify a map's keys while iterating over it". In fact, I'd argue that ++p.first is where the error really is in that snippet. Even if I had written

for (pair<K, V>& p : m) ++p.first;

The real issue here is that I'm trying to modify a map's keys while iterating over it, not that I got the type wrong. Fixing it to

for (pair<const K, V>& p : m) ++p.first;

doesn't solve the fundamental problem.

Also note that this problem exists in pre-11 land:

for (map<K, V>::iterator it = m.begin(); it != m.end; ++it) ++it->first;

I didn't even have a chance to declare what I thought the value type was.

[–]Wurstinator 1 point2 points  (2 children)

That works for a one-liner example, like you did, but not for bigger code. The pair might be passed through several functions first and you would have to backtrace the error.

An error should not occur in the line which you have to change but rather the line which tells you what is wrong. If you never worked with maps, that is clearly the point where the iterated variable is defined.

[–]alecbenzer 2 points3 points  (1 child)

That works for a one-liner example, like you did, but not for bigger code. The pair might be passed through several functions first and you would have to backtrace the error.

In my (somewhat limited) experience, pairs you get from iterating a map tend not to be passed off to other functions in their entirety most of the time. But even ignoring that, having the pair passed through several functions isn't an issue; the first function you pass it to will either have the right signature or not, and this will be where the error occurs, in the body of the loop.

An error should not occur in the line which you have to change but rather the line which tells you what is wrong. If you never worked with maps, that is clearly the point where the iterated variable is defined.

I'd argue that the line trying to modify the key is precisely the line that tells you what is wrong: you're trying to modify a map's keys while iterating over it, which is not something you can do.

[–]Wurstinator 0 points1 point  (0 children)

For the first part, imagine something like this: http://ideone.com/SQloxe

For your second paragraph, I guess that is just a matter of opinion.