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

C++Visual Studio-2010TuplesStd PairStdtuple

C++ Problem Overview


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...)

C++ Solutions


Solution 1 - C++

There are some differences:

  1. std::tuple is not required by the standard to ever be standard-layout. 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.

Solution 2 - C++

An std::tuple's name is longer (one extra character). More of those characters are typed with the right hand, so easier for most people to type.

That said, std::pair can only have two values - not zero, one, three or more. TWO values. A tuple, however, has almost no semantic limitation on the number of values. An std::pair, therefore, is a more accurate, type safe type to use if you actually want to specify a pair of values.

Solution 3 - C++

This is a very late answer but note that, because std::pair is defined with member variables, its size cannot be optimized using empty base class optimization (first and second must occupy distinct addresses, even if one or both is an empty class). This exacerbated by whatever alignment requirements second_type has, so in the worst case the resulting std::pair will be basically twice the size it needs to be.

std::tuple only allows access through helper functions, so it's possible for it to derive from either type if one or the other is empty, saving on the overhead. GCC's implementation, at very least, definitely does this...you can poke through the headers to verify this but there's also this as evidence.

Solution 4 - C++

Note that with C++ 17, one can use the same interface to read data from both pair and tuple with two elements.

auto [a, b] = FunctionToReturnPairOrTuple();

No need to use get<> :)

Solution 5 - C++

For what it's worth, I find the GDB output of std::tuple to be far more difficult to read. Obviously if you need more than 2 values then std::pair won't work, but I do consider this a point in favor of structs.

Solution 6 - C++

It is, perhaps, worth noting that cppreference states:

"A pair is a specific case of a std::tuple with two elements."

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCaseyView Question on Stackoverflow
Solution 1 - C++Nicol BolasView Answer on Stackoverflow
Solution 2 - C++ArafangionView Answer on Stackoverflow
Solution 3 - C++Stephen LinView Answer on Stackoverflow
Solution 4 - C++bhardwajsView Answer on Stackoverflow
Solution 5 - C++tgoodhartView Answer on Stackoverflow
Solution 6 - C++Eoin RoeView Answer on Stackoverflow