23 General utilities library [utilities]

23.5 Tuples [tuple]

23.5.3 Class template tuple [tuple.tuple] Tuple creation functions [tuple.creation]

In the function descriptions that follow, the members of a parameter pack XTypes are denoted by Xi for i in [0, sizeof...(XTypes)) in order, where indexing is zero-based.

template<class... TTypes> constexpr tuple<VTypes...> make_tuple(TTypes&&... t);

The pack VTypes is defined as follows. Let Ui be decay_­t<Ti> for each Ti in TTypes. If Ui is a specialization of reference_­wrapper, then Vi in VTypes is Ui​::​type&, otherwise Vi is Ui.

Returns: tuple<VTypes...>(std​::​forward<TTypes>(t)...).


int i; float j;
make_tuple(1, ref(i), cref(j))

creates a tuple of type tuple<int, int&, const float&>. end example]

template<class... TTypes> constexpr tuple<TTypes&&...> forward_as_tuple(TTypes&&... t) noexcept;

Effects: Constructs a tuple of references to the arguments in t suitable for forwarding as arguments to a function. Because the result may contain references to temporary variables, a program shall ensure that the return value of this function does not outlive any of its arguments (e.g., the program should typically not store the result in a named variable).

Returns: tuple<TTypes&&...>(std​::​forward<TTypes>(t)...).

template<class... TTypes> constexpr tuple<TTypes&...> tie(TTypes&... t) noexcept;

Returns: tuple<TTypes&...>(t...). When an argument in t is ignore, assigning any value to the corresponding tuple element has no effect.

[Example: tie functions allow one to create tuples that unpack tuples into variables. ignore can be used for elements that are not needed:

int i; std::string s;
tie(i, ignore, s) = make_tuple(42, 3.14, "C++");
// i == 42, s == "C++"

end example]

template <class... Tuples> constexpr tuple<CTypes...> tuple_cat(Tuples&&... tpls);

In the following paragraphs, let Ti be the ith type in Tuples, Ui be remove_­reference_­t<Ti>, and tpi be the ith parameter in the function parameter pack tpls, where all indexing is zero-based.

Requires: For all i, Ui shall be the type cvi tuple<Argsi...>, where cvi is the (possibly empty) ith cv-qualifier-seq and Argsi is the parameter pack representing the element types in Ui. Let Aik be the kth type in Argsi. For all Aik the following requirements shall be satisfied:

  • If Ti is deduced as an lvalue reference type, then is_­constructible_­v<Aik, cviAik&> == true, otherwise

  • is_­constructible_­v<Aik, cviAik&&> == true.

Remarks: The types in CTypes shall be equal to the ordered sequence of the extended types Args0..., Args1..., , Argsn1..., where n is equal to sizeof...(Tuples). Let ei... be the ith ordered sequence of tuple elements of the resulting tuple object corresponding to the type sequence Argsi.

Returns: A tuple object constructed by initializing the kith type element eik in ei... with


for each valid ki and each group ei in order.

[Note: An implementation may support additional types in the parameter pack Tuples that support the tuple-like protocol, such as pair and array. end note]