# std::pair

> C++ Atlas: `cppatlas.com/w/cpp/utility/pair` | Source: https://en.cppreference.com/w/cpp/utility/pair | Domain: Utility | Standards: C++11, C++17, C++20, C++23

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.

## Official Specification Summary (cppreference)

```cpp
// Defined in header <utility>

template<
    class T1,
    class T2
> struct pair;
```

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

If neither `T1` nor `T2` is a possibly cv-qualified class type with non-trivial destructor, or array thereof, the destructor of `pair` is trivial.

### Template parameters

- `T1, T2` - the types of the elements that the pair stores.

### Member types

**Member type — Definition**

- `first_type`: `T1`
- `second_type`: `T2`

### Member objects

**Member name — Type**

- `first`: `T1`
- `second`: `T2`

### Member functions

- (constructor): constructs new `pair` (public member function)
- `operator=`: assigns the contents (public member function)
- `swap` (C++11): swaps the contents (public member function)

### Non-member functions

- `make_pair`: creates a `pair` object of type, determined by the argument types (function template)
- `operator==`, `operator!=` (removed in C++20), `operator<` (removed in C++20), `operator<=` (removed in C++20), `operator>` (removed in C++20), `operator>=` (removed in C++20), `operator<=>` (C++20): lexicographically compares the values in the `pair` (function template)
- `std::swap(std::pair)` (C++11): specializes the `std::swap` algorithm (function template)
- `get(std::pair)` (C++11): accesses an element of a `pair` (function template)

### Helper classes

- `std::tuple_size<std::pair>` (C++11): obtains the size of a `pair` (class template specialization)
- `std::tuple_element<std::pair>` (C++11): obtains the type of the elements of `pair` (class template specialization)
- `std::basic_common_reference<std::pair>` (C++23): determines the common reference type of two `pair`s (class template specialization)
- `std::common_type<std::pair>` (C++23): determines the common type of two `pair`s (class template specialization)
- `std::formatter<std::pair>` (C++23): formatting support for `pair` (class template specialization)

### Helper specializations

```cpp
template< class T, class U >
constexpr bool enable_nonlocking_formatter_optimization<std::pair<T, U>>
 = enable_nonlocking_formatter_optimization<T> &&
   enable_nonlocking_formatter_optimization<U>; // (since C++23)
```

This specialization of `std::enable_nonlocking_formatter_optimization` enables efficient implementation of `std::print` and `std::println` for printing a `pair` object when both `T` and `U` enable it.

### Deduction guides (since C++17)

### Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

| DR | Applied to | Behavior as published | Correct behavior |
| --- | --- | --- | --- |
| LWG 2796 | C++98 | triviality of the destructor of `pair` was unspecified | specified |

### See also

- [`tuple`](https://en.cppreference.com/w/cpp/utility/tuple) (C++11): implements fixed size container, which holds elements of possibly different types (class template)
- [`tie`](https://en.cppreference.com/w/cpp/utility/tuple/tie) (C++11): creates a `tuple` of lvalue references or unpacks a tuple into individual objects (function template)

---
Attribution: content from https://en.cppreference.com/w/cpp/utility/pair (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.