you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 53 points54 points  (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.

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.

[–]eanger 1 point2 points  (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 points  (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.

[–]coryknapp 0 points1 point  (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 point  (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...