Difference between std::pair and std::tuple with only two members?

Casey picture Casey · Jul 14, 2011 · Viewed 50.7k times · Source

Is there a difference between an std::pair and an std::tuple with only two members? (Besides the obvious that std::pair requires two and only two members and tuple may have more or less...)

Answer

Nicol Bolas picture Nicol Bolas · Jul 14, 2011

There are some differences:

  1. std::tuple can never be by standard-layout (at least, it's not required to be by the standard). Every std::pair<T, Y> is standard-layout if both T and Y are standard-layout.

  2. It's a bit easier to get the contents of a pair than a tuple. You have to use a function call in the tuple case, while the pair case is just a member field.

But that's about it.