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...)
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. 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.
A tuple is an object capable to hold a collection of elements where each element can be of a different type. The class template needs the header <tuple> . std::tuple is a generalization of std::pair. You can convert between tuples with two elements and pairs.
std::pair is a class template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a std::tuple with two elements.
Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair.
There are some differences:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With