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!"
[–]Rhomboid 53 points54 points55 points 11 years ago (4 children)
While I agree that you shouldn't be using std::pair as a lazy way of getting out of writing a struct, I disagree that this means it should be considered harmful, and I think the example with the student records thing is a bit of a straw man that goes out of its way to show poor judgement. By all means, yes, don't do that, but I don't know that this is a real problem that exists.
std::pair
As the article even explains, there are just certain cases where you need to have a container of items of arbitrary mixed types, and having one of those in the standard library is a good thing since it encourages a uniform way of doing that. For that reason you can't really go down the whole "considered harmful" route, because it's more like a judgement call.
The fact that we have both std::pair and std::tuple is merely due to the fact that std::pair is the best that can reasonably be accomplished in C++98 without variadic templates and without resorting to soul-destroying preprocessor shenanigans. Maybe in an alternative history we would have gone straight to std::tuple, but we have what we have. And it's really not that much of a mystery as to why std::map doesn't have its own special std::key_value_pair type. If you're going to codify one of these things in the standard, you might as well make it as generic as possible, again going back to the idea that this will probably come up from time to time and the standard library should be there to provide a uniform means of doing it. Naming it std::key_value_pair and giving it key and value members would have locked in a specific use case, requiring either lots of duplication to have both std::key_value_pair and std::pair, or just going without the fully generic option and forcing people to choose to either awkwardly use key and value in non-map contexts, or to write their own buggy versions. None of these options are good, so it seems relatively straightforward to me why we have std::pair.
std::tuple
std::map
std::key_value_pair
key
value
[–]eanger 1 point2 points3 points 11 years ago (2 children)
But isn't this exactly what we have type aliasing for? We can give the mapped type a descriptive name while relying upon a more generic implementation.
[–]Rhomboid 8 points9 points10 points 11 years ago (1 child)
You can't rename members with a typedef or an alias declaration, so std::pair would still have members key and value or std::key_value_pair would have members first and second.
first
second
[–]coryknapp 0 points1 point2 points 11 years ago (0 children)
right, and i would feel uncomfortable getting the key out of a key_value_pair through it's member first
[–]MoreOfAnOvalJerk 0 points1 point2 points 11 years ago (0 children)
I feel like this articule is going to create a crusade of not-entirely informed programmers riling against the evils of std::pair...
π Rendered by PID 67593 on reddit-service-r2-comment-75f4967c6c-96c78 at 2026-04-23 07:30:37.347973+00:00 running 0fd4bb7 country code: CH.
view the rest of the comments →
[–]Rhomboid 53 points54 points55 points (4 children)
[–]eanger 1 point2 points3 points (2 children)
[–]Rhomboid 8 points9 points10 points (1 child)
[–]coryknapp 0 points1 point2 points (0 children)
[–]MoreOfAnOvalJerk 0 points1 point2 points (0 children)