Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 628
Casey Avatar asked Jul 14 '11 00:07

Casey


People also ask

What is the difference between tuple and pair?

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.

Is std :: pair a tuple?

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.

What is std :: pair used for?

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.

What is std :: tuple?

Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair.


1 Answers

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.

like image 68
Nicol Bolas Avatar answered Sep 23 '22 01:09

Nicol Bolas