22 General utilities library [utilities] (original) (raw)

22.3.1 General [pairs.general]

The library provides a template for heterogeneous pairs of values.

The library also provides a matching function template to simplify their construction and several templates that provide access to pairobjects as if they were tuple objects (see [tuple.helper]and [tuple.elem]).

22.3.2 Class template pair [pairs.pair]

namespace std { template<class T1, class T2> struct pair { using first_type = T1;using second_type = T2; T1 first; T2 second; pair(const pair&) = default; pair(pair&&) = default;constexpr explicit(see below) pair();constexpr explicit(see below) pair(const T1& x, const T2& y);template<class U1 = T1, class U2 = T2> constexpr explicit(see below) pair(U1&& x, U2&& y);template<class U1, class U2> constexpr explicit(see below) pair(pair<U1, U2>& p);template<class U1, class U2> constexpr explicit(see below) pair(const pair<U1, U2>& p);template<class U1, class U2> constexpr explicit(see below) pair(pair<U1, U2>&& p);template<class U1, class U2> constexpr explicit(see below) pair(const pair<U1, U2>&& p);template<pair-like P> constexpr explicit(see below) pair(P&& p);template<class... Args1, class... Args2> constexpr pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args);constexpr pair& operator=(const pair& p);constexpr const pair& operator=(const pair& p) const;template<class U1, class U2> constexpr pair& operator=(const pair<U1, U2>& p);template<class U1, class U2> constexpr const pair& operator=(const pair<U1, U2>& p) const;constexpr pair& operator=(pair&& p) noexcept(see below);constexpr const pair& operator=(pair&& p) const;template<class U1, class U2> constexpr pair& operator=(pair<U1, U2>&& p);template<class U1, class U2> constexpr const pair& operator=(pair<U1, U2>&& p) const;template<pair-like P> constexpr pair& operator=(P&& p);template<pair-like P> constexpr const pair& operator=(P&& p) const;constexpr void swap(pair& p) noexcept(see below);constexpr void swap(const pair& p) const noexcept(see below);};template<class T1, class T2> pair(T1, T2) -> pair<T1, T2>;}

Constructors and member functions of pair do not throw exceptions unless one of the element-wise operations specified to be called for that operation throws an exception.

The defaulted move and copy constructor, respectively, of pairis a constexpr function if and only if all required element-wise initializations for move and copy, respectively, would be constexpr-suitable ([dcl.constexpr]).

If (is_trivially_destructible_v<T1> && is_trivially_destructible_v<T2>)is true, then the destructor of pair is trivial.

pair<T, U> is a structural type ([temp.param]) if T and U are both structural types.

Two values p1 and p2 of type pair<T, U>are template-argument-equivalent ([temp.type]) if and only ifp1.first and p2.first are template-argument-equivalent andp1.second and p2.second are template-argument-equivalent.

constexpr explicit(_see below_) pair();

Constraints:

Effects: Value-initializes first and second.

Remarks: The expression inside explicit evaluates to trueif and only if either T1 orT2 is not implicitly default-constructible.

[Note 1:

This behavior can be implemented with a trait that checks whether a const T1& or a const T2&can be initialized with {}.

— _end note_]

constexpr explicit(_see below_) pair(const T1& x, const T2& y);

Constraints:

Effects: Initializes first with x and second with y.

Remarks: The expression inside explicit is equivalent to:!is_convertible_v<const T1&, T1> || !is_convertible_v<const T2&, T2>

template<class U1 = T1, class U2 = T2> constexpr explicit(_see below_) pair(U1&& x, U2&& y);

Constraints:

Effects: Initializes first withstd​::​forward<U1>(x) and secondwith std​::​forward<U2>(y).

Remarks: The expression inside explicit is equivalent to:!is_convertible_v<U1, T1> || !is_convertible_v<U2, T2>

This constructor is defined as deleted ifreference_constructs_from_temporary_v<first_type, U1&&>is true orreference_constructs_from_temporary_v<second_type, U2&&>is true.

template<class U1, class U2> constexpr explicit(_see below_) pair(pair<U1, U2>& p);template<class U1, class U2> constexpr explicit(_see below_) pair(const pair<U1, U2>& p);template<class U1, class U2> constexpr explicit(_see below_) pair(pair<U1, U2>&& p);template<class U1, class U2> constexpr explicit(_see below_) pair(const pair<U1, U2>&& p);template<[_pair-like_](tuple.syn#concept:pair-like "22.4.2 Header <tuple> synopsis [tuple.syn]") P> constexpr explicit(_see below_) pair(P&& p);

Let FWD(u) be static_cast<decltype(u)>(u).

Constraints:

Effects: Initializes first with get<0>(FWD(p)) andsecond with get<1>(FWD(p)).

Remarks: The expression inside explicit is equivalent to:!is_convertible_v<decltype(get<0>(FWD(p))), T1> || !is_convertible_v<decltype(get<1>(FWD(p))), T2>

The constructor is defined as deleted ifreference_constructs_from_temporary_v<first_type, decltype(get<0>(FWD(p)))> ||reference_constructs_from_temporary_v<second_type, decltype(get<1>(FWD(p)))> is true.

template<class... Args1, class... Args2> constexpr pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args);

Mandates:

Effects: Initializes first with arguments of typesArgs1... obtained by forwarding the elements of first_argsand initializes second with arguments of types Args2...obtained by forwarding the elements of second_args.

(Here, forwarding an element x of type U within a tuple object means callingstd​::​forward<U>(x).)

This form of construction, whereby constructor arguments for first and second are each provided in a separatetuple object, is called piecewise construction.

[Note 2:

If a data member of pair is of reference type and its initialization binds it to a temporary object, the program is ill-formed ([class.base.init]).

— _end note_]

constexpr pair& operator=(const pair& p);

Effects: Assigns p.first to first and p.second to second.

Remarks: This operator is defined as deleted unlessis_copy_assignable_v<T1> is true andis_copy_assignable_v<T2> is true.

constexpr const pair& operator=(const pair& p) const;

Constraints:

Effects: Assigns p.first to first and p.second to second.

template<class U1, class U2> constexpr pair& operator=(const pair<U1, U2>& p);

Constraints:

Effects: Assigns p.first to first and p.second to second.

template<class U1, class U2> constexpr const pair& operator=(const pair<U1, U2>& p) const;

Constraints:

Effects: Assigns p.first to first and p.second to second.

constexpr pair& operator=(pair&& p) noexcept(_see below_);

Constraints:

Effects: Assigns std​::​forward<T1>(p.first) to first andstd​::​forward<T2>(p.second) to second.

Remarks: The exception specification is equivalent to:is_nothrow_move_assignable_v<T1> && is_nothrow_move_assignable_v<T2>

constexpr const pair& operator=(pair&& p) const;

Constraints:

Effects: Assigns std​::​forward<T1>(p.first) to first andstd​::​forward<T2>(p.second) to second.

template<class U1, class U2> constexpr pair& operator=(pair<U1, U2>&& p);

Constraints:

Effects: Assigns std​::​forward<U1>(p.first) first andstd​::​forward<U2>(p.second) to second.

template<[_pair-like_](tuple.syn#concept:pair-like "22.4.2 Header <tuple> synopsis [tuple.syn]") P> constexpr pair& operator=(P&& p);

Constraints:

Effects: Assigns get<0>(std​::​forward<P>(p)) to first andget<1>(std​::​forward<P>(p)) to second.

template<[_pair-like_](tuple.syn#concept:pair-like "22.4.2 Header <tuple> synopsis [tuple.syn]") P> constexpr const pair& operator=(P&& p) const;

Constraints:

Effects: Assigns get<0>(std​::​forward<P>(p)) to first andget<1>(std​::​forward<P>(p)) to second.

template<class U1, class U2> constexpr const pair& operator=(pair<U1, U2>&& p) const;

Constraints:

Effects: Assigns std​::​forward<U1>(p.first) to first andstd​::​forward<U2>(u.second) to second.

constexpr void swap(pair& p) noexcept(_see below_);constexpr void swap(const pair& p) const noexcept(_see below_);

Mandates:

Effects: Swapsfirst with p.first andsecond with p.second.

Remarks: The exception specification is equivalent to:

22.3.3 Specialized algorithms [pairs.spec]

template<class T1, class T2, class U1, class U2> constexpr bool operator==(const pair<T1, T2>& x, const pair<U1, U2>& y);

Constraints: x.first == y.first and x.second == y.second are valid expressions and each of decltype(x.first == y.first) anddecltype(x.second == y.second) models boolean- testable.

Returns: x.first == y.first && x.second == y.second.

template<class T1, class T2, class U1, class U2> constexpr common_comparison_category_t<_synth-three-way-result_<T1, U1>,_synth-three-way-result_<T2, U2>> operator<=>(const pair<T1, T2>& x, const pair<U1, U2>& y);

Effects: Equivalent to:if (auto c = synth-three-way(x.first, y.first); c != 0) return c;return synth-three-way(x.second, y.second);

template<class T1, class T2> constexpr void swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));template<class T1, class T2> constexpr void swap(const pair<T1, T2>& x, const pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));

Constraints:

Effects: Equivalent to x.swap(y).

template<class T1, class T2> constexpr pair<unwrap_ref_decay_t<T1>, unwrap_ref_decay_t<T2>> make_pair(T1&& x, T2&& y);

Returns: pair<unwrap_ref_decay_t<T1>, unwrap_ref_decay_t<T2>>(std::forward<T1>(x), std::forward<T2>(y))

[Example 1:

In place of:return pair<int, double>(5, 3.1415926); a C++ program may contain:return make_pair(5, 3.1415926);

— _end example_]

22.3.4 Tuple-like access to pair [pair.astuple]

template<class T1, class T2> struct tuple_size<pair<T1, T2>> : integral_constant<size_t, 2> { };

template<size_t I, class T1, class T2> struct tuple_element<I, pair<T1, T2>> { using type = _see below_ ;};

Type: The type T1 if I is 0, otherwise the type T2.

template<size_t I, class T1, class T2> constexpr tuple_element_t<I, pair<T1, T2>>& get(pair<T1, T2>& p) noexcept;template<size_t I, class T1, class T2> constexpr const tuple_element_t<I, pair<T1, T2>>& get(const pair<T1, T2>& p) noexcept;template<size_t I, class T1, class T2> constexpr tuple_element_t<I, pair<T1, T2>>&& get(pair<T1, T2>&& p) noexcept;template<size_t I, class T1, class T2> constexpr const tuple_element_t<I, pair<T1, T2>>&& get(const pair<T1, T2>&& p) noexcept;

Returns:

template<class T1, class T2> constexpr T1& get(pair<T1, T2>& p) noexcept;template<class T1, class T2> constexpr const T1& get(const pair<T1, T2>& p) noexcept;template<class T1, class T2> constexpr T1&& get(pair<T1, T2>&& p) noexcept;template<class T1, class T2> constexpr const T1&& get(const pair<T1, T2>&& p) noexcept;

Mandates: T1 and T2 are distinct types.

Returns: A reference to p.first.

template<class T2, class T1> constexpr T2& get(pair<T1, T2>& p) noexcept;template<class T2, class T1> constexpr const T2& get(const pair<T1, T2>& p) noexcept;template<class T2, class T1> constexpr T2&& get(pair<T1, T2>&& p) noexcept;template<class T2, class T1> constexpr const T2&& get(const pair<T1, T2>&& p) noexcept;

Mandates: T1 and T2 are distinct types.

Returns: A reference to p.second.

22.3.5 Piecewise construction [pair.piecewise]

struct piecewise_construct_t { explicit piecewise_construct_t() = default;};inline constexpr piecewise_construct_t piecewise_construct{};

The struct piecewise_construct_t is an empty class type used as a unique type to disambiguate constructor and function overloading.

Specifically,pair has a constructor with piecewise_construct_t as the first argument, immediately followed by two arguments used for piecewise construction of the elements of the pair object.