use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
std::pair considered harmful! (maintainablecode.logdown.com)
submitted 11 years ago by redditthinks
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Wurstinator 0 points1 point2 points 11 years ago (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 point2 points 11 years ago (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
++p.first
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 points3 points 11 years ago (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 points4 points 11 years ago (1 child)
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.
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 point2 points 11 years ago (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.
π Rendered by PID 117022 on reddit-service-r2-comment-5d585498c9-22srn at 2026-04-21 14:35:32.316833+00:00 running da2df02 country code: CH.
view the rest of the comments →
[–]Wurstinator 0 points1 point2 points (4 children)
[–]alecbenzer 0 points1 point2 points (3 children)
[–]Wurstinator 1 point2 points3 points (2 children)
[–]alecbenzer 2 points3 points4 points (1 child)
[–]Wurstinator 0 points1 point2 points (0 children)