66 KiB
[tuple]
22 General utilities library [utilities]
22.4 Tuples [tuple]
22.4.1 General [tuple.general]
Subclause [tuple] describes the tuple library that provides a tuple type as the class template tuple that can be instantiated with any number of arguments.
Each template argument specifies the type of an element in the tuple.
Consequently, tuples are heterogeneous, fixed-size collections of values.
An instantiation of tuple with two arguments is similar to an instantiation of pair with the same two arguments.
In addition to being available via inclusion of the header,ignore ([tuple.syn]) is available when ([utility]) is included.
22.4.2 Header synopsis [tuple.syn]
// all freestanding#include // see [compare.syn]namespace std {// [tuple.tuple], class template tupletemplate<class... Types>class tuple; // [tuple.like], concept tuple-liketemplateconcept tuple-like = see below; // exposition onlytemplateconcept pair-like = // exposition onlytuple-like && tuple_size_v<remove_cvref_t> == 2; // [tuple.common.ref], common_reference related specializationstemplate<tuple-like TTuple, tuple-like UTuple, template class TQual, template class UQual>struct basic_common_reference<TTuple, UTuple, TQual, UQual>; template<tuple-like TTuple, tuple-like UTuple>struct common_type<TTuple, UTuple>; // ignorestruct ignore-type { // exposition onlyconstexpr const ignore-type&operator=(const auto &) const noexcept { return *this; }}; inline constexpr ignore-type ignore; // [tuple.creation], tuple creation functionstemplate<class... TTypes>constexpr tuple<unwrap_ref_decay_t...> make_tuple(TTypes&&...); template<class... TTypes>constexpr tuple<TTypes&&...> forward_as_tuple(TTypes&&...) noexcept; template<class... TTypes>constexpr tuple<TTypes&...> tie(TTypes&...) noexcept; template<tuple-like... Tuples>constexpr tuple<CTypes...> tuple_cat(Tuples&&...); // [tuple.apply], calling a function with a tuple of argumentstemplate<class F, tuple-like Tuple>constexpr apply_result_t<F, Tuple> apply(F&& f, Tuple&& t)noexcept(is_nothrow_applicable_v<F, Tuple>); template<class T, tuple-like Tuple>constexpr T make_from_tuple(Tuple&& t); // [tuple.helper], tuple helper classestemplate struct tuple_size; // not definedtemplate struct tuple_size; template<class... Types> struct tuple_size<tuple<Types...>>; template<size_t I, class T> struct tuple_element; // not definedtemplate<size_t I, class T> struct tuple_element<I, const T>; template<size_t I, class... Types>struct tuple_element<I, tuple<Types...>>; template<size_t I, class T>using tuple_element_t = typename tuple_element<I, T>::type; // [tuple.elem], element accesstemplate<size_t I, class... Types>constexpr tuple_element_t<I, tuple<Types...>>& get(tuple<Types...>&) noexcept; template<size_t I, class... Types>constexpr tuple_element_t<I, tuple<Types...>>&& get(tuple<Types...>&&) noexcept; template<size_t I, class... Types>constexpr const tuple_element_t<I, tuple<Types...>>& get(const tuple<Types...>&) noexcept; template<size_t I, class... Types>constexpr const tuple_element_t<I, tuple<Types...>>&& get(const tuple<Types...>&&) noexcept; template<class T, class... Types>constexpr T& get(tuple<Types...>& t) noexcept; template<class T, class... Types>constexpr T&& get(tuple<Types...>&& t) noexcept; template<class T, class... Types>constexpr const T& get(const tuple<Types...>& t) noexcept; template<class T, class... Types>constexpr const T&& get(const tuple<Types...>&& t) noexcept; // [tuple.rel], relational operatorstemplate<class... TTypes, class... UTypes>constexpr bool operator==(const tuple<TTypes...>&, const tuple<UTypes...>&); template<class... TTypes, tuple-like UTuple>constexpr bool operator==(const tuple<TTypes...>&, const UTuple&); template<class... TTypes, class... UTypes>constexpr common_comparison_category_t<synth-three-way-result<TTypes, UTypes>...>operator<=>(const tuple<TTypes...>&, const tuple<UTypes...>&); template<class... TTypes, tuple-like UTuple>constexpr see below operator<=>(const tuple<TTypes...>&, const UTuple&); // [tuple.traits], allocator-related traitstemplate<class... Types, class Alloc>struct uses_allocator<tuple<Types...>, Alloc>; // [tuple.special], specialized algorithmstemplate<class... Types>constexpr void swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(see below); template<class... Types>constexpr void swap(const tuple<Types...>& x, const tuple<Types...>& y) noexcept(see below); // [tuple.helper], tuple helper classestemplateconstexpr size_t tuple_size_v = tuple_size::value;}
22.4.3 Concept tuple-like [tuple.like]
template<class T> concept [tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") = see below; // exposition only
A type T models and satisfies the exposition-only concept tuple-like if remove_cvref_t is a specialization ofarray, complex, pair, tuple, or ranges::subrange.
22.4.4 Class template tuple [tuple.tuple]
22.4.4.1 General [tuple.tuple.general]
namespace std {template<class... Types>class tuple {public:// [tuple.cnstr], tuple constructionconstexpr explicit(see below) tuple(); constexpr explicit(see below) tuple(const Types&...); // only if sizeof...(Types) >= 1template<class... UTypes>constexpr explicit(see below) tuple(UTypes&&...); // only if sizeof...(Types) >= 1 tuple(const tuple&) = default; tuple(tuple&&) = default; template<class... UTypes>constexpr explicit(see below) tuple(tuple<UTypes...>&); template<class... UTypes>constexpr explicit(see below) tuple(const tuple<UTypes...>&); template<class... UTypes>constexpr explicit(see below) tuple(tuple<UTypes...>&&); template<class... UTypes>constexpr explicit(see below) tuple(const tuple<UTypes...>&&); template<class U1, class U2>constexpr explicit(see below) tuple(pair<U1, U2>&); // only if sizeof...(Types) == 2template<class U1, class U2>constexpr explicit(see below) tuple(const pair<U1, U2>&); // only if sizeof...(Types) == 2template<class U1, class U2>constexpr explicit(see below) tuple(pair<U1, U2>&&); // only if sizeof...(Types) == 2template<class U1, class U2>constexpr explicit(see below) tuple(const pair<U1, U2>&&); // only if sizeof...(Types) == 2template<tuple-like UTuple>constexpr explicit(see below) tuple(UTuple&&); // allocator-extended constructorstemplateconstexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a); templateconstexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const Types&...); template<class Alloc, class... UTypes>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, UTypes&&...); templateconstexpr tuple(allocator_arg_t, const Alloc& a, const tuple&); templateconstexpr tuple(allocator_arg_t, const Alloc& a, tuple&&); template<class Alloc, class... UTypes>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&); template<class Alloc, class... UTypes>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&); template<class Alloc, class... UTypes>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&&); template<class Alloc, class... UTypes>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&&); template<class Alloc, class U1, class U2>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&); template<class Alloc, class U1, class U2>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&); template<class Alloc, class U1, class U2>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&&); template<class Alloc, class U1, class U2>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&&); template<class Alloc, tuple-like UTuple>constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, UTuple&&); // [tuple.assign], tuple assignmentconstexpr tuple& operator=(const tuple&); constexpr const tuple& operator=(const tuple&) const; constexpr tuple& operator=(tuple&&) noexcept(see below); constexpr const tuple& operator=(tuple&&) const; template<class... UTypes>constexpr tuple& operator=(const tuple<UTypes...>&); template<class... UTypes>constexpr const tuple& operator=(const tuple<UTypes...>&) const; template<class... UTypes>constexpr tuple& operator=(tuple<UTypes...>&&); template<class... UTypes>constexpr const tuple& operator=(tuple<UTypes...>&&) const; template<class U1, class U2>constexpr tuple& operator=(const pair<U1, U2>&); // only if sizeof...(Types) == 2template<class U1, class U2>constexpr const tuple& operator=(const pair<U1, U2>&) const; // only if sizeof...(Types) == 2template<class U1, class U2>constexpr tuple& operator=(pair<U1, U2>&&); // only if sizeof...(Types) == 2template<class U1, class U2>constexpr const tuple& operator=(pair<U1, U2>&&) const; // only if sizeof...(Types) == 2template<tuple-like UTuple>constexpr tuple& operator=(UTuple&&); template<tuple-like UTuple>constexpr const tuple& operator=(UTuple&&) const; // [tuple.swap], tuple swapconstexpr void swap(tuple&) noexcept(see below); constexpr void swap(const tuple&) const noexcept(see below); }; template<class... UTypes> tuple(UTypes...) -> tuple<UTypes...>; template<class T1, class T2> tuple(pair<T1, T2>) -> tuple<T1, T2>; template<class Alloc, class... UTypes> tuple(allocator_arg_t, Alloc, UTypes...) -> tuple<UTypes...>; template<class Alloc, class T1, class T2> tuple(allocator_arg_t, Alloc, pair<T1, T2>) -> tuple<T1, T2>; template<class Alloc, class... UTypes> tuple(allocator_arg_t, Alloc, tuple<UTypes...>) -> tuple<UTypes...>;}
If a program declares an explicit or partial specialization of tuple, the program is ill-formed, no diagnostic required.
22.4.4.2 Construction [tuple.cnstr]
In the descriptions that follow, let i be in the range [0, sizeof...(Types)) in order, Ti be the ith type in Types, andUi be the ith type in a template parameter pack named UTypes, where indexing is zero-based.
For each tuple constructor, an exception is thrown only if the construction of one of the types in Types throws an exception.
The defaulted move and copy constructor, respectively, oftuple is a constexpr function if and only if all required element-wise initializations for move and copy, respectively, would be constexpr-suitable ([dcl.constexpr]).
The defaulted move and copy constructor of tuple<> are constexpr functions.
If is_trivially_destructible_v is true for all Ti, then the destructor of tuple is trivial.
The default constructor of tuple<> is trivial.
constexpr explicit(see below) tuple();
Constraints: is_default_constructible_v is true for all i.
Effects: Value-initializes each element.
Remarks: The expression inside explicit evaluates to true if and only if Ti is not copy-list-initializable from an empty list for at least one i.
[Note 1:
This behavior can be implemented with a trait that checks whether a const Ti& can be initialized with {}.
â end note]
constexpr explicit(see below) tuple(const Types&...);
Constraints: sizeof...(Types) ⥠1 andis_copy_constructible_v is true for all i.
Effects: Initializes each element with the value of the corresponding parameter.
Remarks: The expression inside explicit is equivalent to:!conjunction_v<is_convertible<const Types&, Types>...>
template<class... UTypes> constexpr explicit(see below) tuple(UTypes&&... u);
Let disambiguating-constraint be:
negation<is_same<remove_cvref_t, tuple>> if sizeof...(Types) is 1;
otherwise,bool_constant<!is_same_v<remove_cvref_t, allocator_arg_t> || is_-
same_v<remove_cvref_t, allocator_arg_t>> if sizeof...(Types) is 2 or 3;
otherwise, true_type.
Constraints:
sizeof...(Types) equals sizeof...(UTypes),
sizeof...(Types) ⥠1, and
conjunction_v<disambiguating-constraint,
is_constructible<Types, UTypes>...> is
true.
Effects: Initializes the elements in the tuple with the corresponding value in std::forward(u).
Remarks: The expression inside explicit is equivalent to:!conjunction_v<is_convertible<UTypes, Types>...>
This constructor is defined as deleted if(reference_constructs_from_temporary_v<Types, UTypes&&> || ...) is true.
tuple(const tuple& u) = default;
Mandates: is_copy_constructible_v is true for all i.
Effects: Initializes each element of *this with the corresponding element of u.
tuple(tuple&& u) = default;
Constraints: is_move_constructible_v is true for all i.
Effects: For all i, initializes the ith element of *this withstd::forward(get(u)).
template<class... UTypes> constexpr explicit(see below) tuple(tuple<UTypes...>& u); template<class... UTypes> constexpr explicit(see below) tuple(const tuple<UTypes...>& u); template<class... UTypes> constexpr explicit(see below) tuple(tuple<UTypes...>&& u); template<class... UTypes> constexpr explicit(see below) tuple(const tuple<UTypes...>&& u);
Let I be the pack 0, 1, …, (sizeof...(Types) - 1).
Let FWD(u) be static_cast<decltype(u)>(u).
Constraints:
sizeof...(Types) equals sizeof...(UTypes), and
(is_constructible_v<Types, decltype(get(FWD(u)))> && ...) is true, and
either sizeof...(Types) is not 1, or (when Types... expands to T andUTypes... expands to U)is_convertible_v<decltype(u), T>,is_constructible_v<T, decltype(u)>, andis_same_v<T, U> are all false.
Effects: For all i, initializes the ith element of *this with get(FWD(u)).
Remarks: The expression inside explicit is equivalent to:!(is_convertible_v<decltype(get(FWD(u))), Types> && ...)
The constructor is defined as deleted if(reference_constructs_from_temporary_v<Types, decltype(get(FWD(u)))> || ...) is true.
template<class U1, class U2> constexpr explicit(see below) tuple(pair<U1, U2>& u); template<class U1, class U2> constexpr explicit(see below) tuple(const pair<U1, U2>& u); template<class U1, class U2> constexpr explicit(see below) tuple(pair<U1, U2>&& u); template<class U1, class U2> constexpr explicit(see below) tuple(const pair<U1, U2>&& u);
Let FWD(u) be static_cast<decltype(u)>(u).
Constraints:
sizeof...(Types) is 2,
is_constructible_v<T0, decltype(get<0>(FWD(u)))> is true, and
is_constructible_v<T1, decltype(get<1>(FWD(u)))> is true.
Effects: Initializes the first element with get<0>(FWD(u)) and the second element with get<1>(FWD(u)).
Remarks: The expression inside explicit is equivalent to:!is_convertible_v<decltype(get<0>(FWD(u))), T0> ||!is_convertible_v<decltype(get<1>(FWD(u))), T1>
The constructor is defined as deleted ifreference_constructs_from_temporary_v<T0, decltype(get<0>(FWD(u)))> || reference_constructs_from_temporary_v<T1, decltype(get<1>(FWD(u)))> is true.
template<[tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") UTuple> constexpr explicit(see below) tuple(UTuple&& u);
Let I be the pack 0, 1, …, (sizeof...(Types) - 1).
Constraints:
different-from<UTuple, tuple> ([range.utility.helpers]) is true,
remove_cvref_t is not a specialization of ranges::subrange,
sizeof...(Types) equals tuple_size_v<remove_cvref_t>,
(is_constructible_v<Types, decltype(get(std::forward(u)))> && ...) istrue, and
either sizeof...(Types) is not 1, or (when Types... expands to T)is_convertible_v<UTuple, T> andis_constructible_v<T, UTuple> are both false.
Effects: For all i, initializes the ith element of *this withget(std::forward(u)).
Remarks: The expression inside explicit is equivalent to:!(is_convertible_v<decltype(get(std::forward(u))), Types> && ...)
The constructor is defined as deleted if(reference_constructs_from_temporary_v<Types, decltype(get(std::forward(u)))>|| ...) is true.
template<class Alloc> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a); template<class Alloc> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const Types&...); template<class Alloc, class... UTypes> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, UTypes&&...); template<class Alloc> constexpr tuple(allocator_arg_t, const Alloc& a, const tuple&); template<class Alloc> constexpr tuple(allocator_arg_t, const Alloc& a, tuple&&); template<class Alloc, class... UTypes> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&); template<class Alloc, class... UTypes> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&); template<class Alloc, class... UTypes> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&&); template<class Alloc, class... UTypes> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&&); template<class Alloc, class U1, class U2> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&); template<class Alloc, class U1, class U2> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&); template<class Alloc, class U1, class U2> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&&); template<class Alloc, class U1, class U2> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&&); template<class Alloc, [tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") UTuple> constexpr explicit(see below) tuple(allocator_arg_t, const Alloc& a, UTuple&&);
Preconditions: Alloc meets the Cpp17Allocator requirements ([allocator.requirements.general]).
Effects: Equivalent to the preceding constructors except that each element is constructed withuses-allocator construction.
22.4.4.3 Assignment [tuple.assign]
For each tuple assignment operator, an exception is thrown only if the assignment of one of the types in Types throws an exception.
In the function descriptions that follow, let i be in the range [0, sizeof...(Types)) in order, Ti be the ith type in Types, and Ui be the ith type in a template parameter pack named UTypes, where indexing is zero-based.
constexpr tuple& operator=(const tuple& u);
Effects: Assigns each element of u to the corresponding element of *this.
Returns: *this.
Remarks: This operator is defined as deleted unlessis_copy_assignable_v is true for all i.
constexpr const tuple& operator=(const tuple& u) const;
Constraints: (is_copy_assignable_v && ...) is true.
Effects: Assigns each element of u to the corresponding element of *this.
Returns: *this.
constexpr tuple& operator=(tuple&& u) noexcept(see below);
Constraints: is_move_assignable_v is true for all i.
Effects: For all i, assigns std::forward(get(u)) toget(*this).
Returns: *this.
Remarks: The exception specification is equivalent to the logical and of the following expressions:is_nothrow_move_assignable_v where Ti is the ith type in Types.
constexpr const tuple& operator=(tuple&& u) const;
Constraints: (is_assignable_v<const Types&, Types> && ...) is true.
Effects: For all i, assigns std::forward(get(u)) to get(*this).
Returns: *this.
template<class... UTypes> constexpr tuple& operator=(const tuple<UTypes...>& u);
Constraints:
sizeof...(Types) equals sizeof...(UTypes) and
is_assignable_v<Ti&, const Ui&> is true for all i.
Effects: Assigns each element of u to the corresponding element of *this.
Returns: *this.
template<class... UTypes> constexpr const tuple& operator=(const tuple<UTypes...>& u) const;
Constraints:
sizeof...(Types) equals sizeof...(UTypes) and
(is_assignable_v<const Types&, const UTypes&> && ...) is true.
Effects: Assigns each element of u to the corresponding element of *this.
Returns: *this.
template<class... UTypes> constexpr tuple& operator=(tuple<UTypes...>&& u);
Constraints:
sizeof...(Types) equals sizeof...(UTypes) and
is_assignable_v<Ti&, Ui> is true for all i.
Effects: For all i, assigns std::forward(get(u)) toget(*this).
Returns: *this.
template<class... UTypes> constexpr const tuple& operator=(tuple<UTypes...>&& u) const;
Constraints:
sizeof...(Types) equals sizeof...(UTypes) and
(is_assignable_v<const Types&, UTypes> && ...) is true.
Effects: For all i, assigns std::forward(get(u)) to get(*this).
Returns: *this.
template<class U1, class U2> constexpr tuple& operator=(const pair<U1, U2>& u);
Constraints:
sizeof...(Types) is 2 and
is_assignable_v<T0&, const U1&> is true, and
is_assignable_v<T1&, const U2&> is true.
Effects: Assigns u.first to the first element of *this and u.second to the second element of *this.
Returns: *this.
template<class U1, class U2> constexpr const tuple& operator=(const pair<U1, U2>& u) const;
Constraints:
sizeof...(Types) is 2,
is_assignable_v<const T0&, const U1&> is true, and
is_assignable_v<const T1&, const U2&> is true.
Effects: Assigns u.first to the first element andu.second to the second element.
Returns: *this.
template<class U1, class U2> constexpr tuple& operator=(pair<U1, U2>&& u);
Constraints:
sizeof...(Types) is 2 and
is_assignable_v<T0&, U1> is true, and
is_assignable_v<T1&, U2> is true.
Effects: Assigns std::forward(u.first) to the first element of *this and
std::forward(u.second) to the second element of *this.
Returns: *this.
template<class U1, class U2> constexpr const tuple& operator=(pair<U1, U2>&& u) const;
Constraints:
sizeof...(Types) is 2,
is_assignable_v<const T0&, U1> is true, and
is_assignable_v<const T1&, U2> is true.
Effects: Assigns std::forward(u.first) to the first element and
std::forward(u.second) to the second element.
Returns: *this.
template<[tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") UTuple> constexpr tuple& operator=(UTuple&& u);
Constraints:
different-from<UTuple, tuple> ([range.utility.helpers]) is true,
remove_cvref_t is not a specialization of ranges::subrange,
sizeof...(Types) equals tuple_size_v<remove_cvref_t>, and
is_assignable_v<Ti&, decltype(get(std::forward(u)))> is true for all i.
Effects: For all i, assigns get(std::forward(u)) to get(*this).
Returns: *this.
template<[tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") UTuple> constexpr const tuple& operator=(UTuple&& u) const;
Constraints:
different-from<UTuple, tuple> ([range.utility.helpers]) is true,
remove_cvref_t is not a specialization of ranges::subrange,
sizeof...(Types) equals tuple_size_v<remove_cvref_t>, and
is_assignable_v<const Ti&, decltype(get(std::forward(u)))> is true for all i.
Effects: For all i, assignsget(std::forward(u)) to get(*this).
Returns: *this.
22.4.4.4 swap [tuple.swap]
constexpr void swap(tuple& rhs) noexcept(see below); constexpr void swap(const tuple& rhs) const noexcept(see below);
Let i be in the range [0, sizeof...(Types)) in order.
Mandates:
-
For the first overload,(is_swappable_v && ...) is true.
-
For the second overload,(is_swappable_v && ...) is true.
Preconditions: For all i, get(*this) is swappable with ([swappable.requirements]) get(rhs).
Effects: For each i, calls swap for get(*this) and get(rhs).
Throws: Nothing unless one of the element-wise swap calls throws an exception.
Remarks: The exception specification is equivalent to
(is_nothrow_swappable_v && ...) for the first overload and
(is_nothrow_swappable_v && ...) for the second overload.
22.4.5 Tuple creation functions [tuple.creation]
template<class... TTypes> constexpr tuple<unwrap_ref_decay_t<TTypes>...> make_tuple(TTypes&&... t);
Returns: tuple<unwrap_ref_decay_t...>(std::forward(t)...).
[Example 1:
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 objects, 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(t)...).
template<class... TTypes> constexpr tuple<TTypes&...> tie(TTypes&... t) noexcept;
Returns: tuple<TTypes&...>(t...).
[Example 2:
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<[tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]")... Tuples> constexpr tuple<CTypes...> tuple_cat(Tuples&&... tpls);
Let n be sizeof...(Tuples).
For every integer 0â¤i<n:
-
Let Ti be the ith type in Tuples.
-
Let Ui be remove_cvref_t.
-
Let tpi be the ith element in the function parameter pack tpls.
-
Let Si be tuple_size_v.
-
Let Eki be tuple_element_t<k, Ui>.
-
Let eki be get(std::forward(tpi)).
-
Let Elemsi be a pack of the types E0i,â¦,ESiâ1i.
-
Let elemsi be a pack of the expressions e0i,â¦,eSiâ1i.
The types in CTypes are equal to the ordered sequence of the expanded packs of typesElems0..., Elems1..., …, Elemsnâ1....
Let celems be the ordered sequence of the expanded packs of expressionselems0..., …, elemsnâ1....
Mandates: (is_constructible_v<CTypes, decltype(celems)> && ...) is true.
Returns: tuple<CTypes...>(celems...).
22.4.6 Calling a function with a tuple of arguments [tuple.apply]
template<class F, [tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") Tuple> constexpr apply_result_t<F, Tuple> apply(F&& f, Tuple&& t) noexcept(is_nothrow_applicable_v<F, Tuple>);
Effects: Given the exposition-only function template:namespace std {template<class F, tuple-like Tuple, size_t... I>constexpr decltype(auto) apply-impl(F&& f, Tuple&& t, index_sequence<I...>) {// exposition onlyreturn INVOKE(std::forward(f), get(std::forward(t))...); // see [func.require]}}
Equivalent to:return apply-impl(std::forward(f), std::forward(t), make_index_sequence<tuple_size_v<remove_reference_t>>{});
template<class T, [tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") Tuple> constexpr T make_from_tuple(Tuple&& t);
Mandates: If tuple_size_v<remove_reference_t> is 1, thenreference_constructs_from_temporary_v<T, decltype(get<0>(declval()))> is false.
Effects: Given the exposition-only function template:namespace std {template<class T, tuple-like Tuple, size_t... I>requires is_constructible_v<T, decltype(get(declval()))...>constexpr T make-from-tuple-impl(Tuple&& t, index_sequence<I...>) { // exposition onlyreturn T(get(std::forward(t))...); }}
Equivalent to:return make-from-tuple-impl( std::forward(t), make_index_sequence<tuple_size_v<remove_reference_t>>{});
[Note 1:
The type of T must be supplied as an explicit template parameter, as it cannot be deduced from the argument list.
â end note]
22.4.7 Tuple helper classes [tuple.helper]
template<class T> struct tuple_size;
Except where specified otherwise, all specializations of tuple_size meet theCpp17UnaryTypeTrait requirements ([meta.rqmts]) with a base characteristic of integral_constant<size_t, N> for some N.
template<class... Types> struct tuple_size<tuple<Types...>> : integral_constant<size_t, sizeof...(Types)> { };
template<size_t I, class... Types> struct tuple_element<I, tuple<Types...>> { using type = TI; };
Mandates: I < sizeof...(Types).
Type: TI is the type of the Ith element of Types, where indexing is zero-based.
template<class T> struct tuple_size<const T>;
Let TS denote tuple_size of the cv-unqualified type T.
If the expression TS::value is well-formed when treated as an unevaluated operand, then each specialization of the template meets the Cpp17UnaryTypeTrait requirements ([meta.rqmts]) with a base characteristic ofintegral_constant<size_t, TS::value>
Otherwise, it has no member value.
Access checking is performed as if in a context unrelated to TS and T.
Only the validity of the immediate context of the expression is considered.
[Note 1:
The compilation of the expression can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on.
Such side effects are not in the âimmediate contextâ and can result in the program being ill-formed.
â end note]
In addition to being available via inclusion of the header, the template is available when any of the headers,, or are included.
template<size_t I, class T> struct tuple_element<I, const T>;
Let TE denote tuple_element_t<I, T> of the cv-unqualified type T.
Then each specialization of the template meets the Cpp17TransformationTrait requirements ([meta.rqmts]) with a member typedef type that names the type add_const_t.
In addition to being available via inclusion of the header, the template is available when any of the headers,, or are included.
22.4.8 Element access [tuple.elem]
template<size_t I, class... Types> constexpr tuple_element_t<I, tuple<Types...>>& get(tuple<Types...>& t) noexcept; template<size_t I, class... Types> constexpr tuple_element_t<I, tuple<Types...>>&& get(tuple<Types...>&& t) noexcept; // #1 template<size_t I, class... Types> constexpr const tuple_element_t<I, tuple<Types...>>& get(const tuple<Types...>& t) noexcept; // #2 template<size_t I, class... Types> constexpr const tuple_element_t<I, tuple<Types...>>&& get(const tuple<Types...>&& t) noexcept;
Mandates: I < sizeof...(Types).
Returns: A reference to the Ith element of t, where indexing is zero-based.
[Note 1:
For the overload marked #1, if a type T in Types is some reference type X&, the return type is X&, not X&&.
However, if the element type is a non-reference type T, the return type is T&&.
â end note]
[Note 2:
Constness is shallow.
For the overload marked #2, if a type T in Types is some reference type X&, the return type is X&, not const X&.
However, if the element type is a non-reference type T, the return type is const T&.
This is consistent with how constness is defined to work for non-static data members of reference type.
â end note]
template<class T, class... Types> constexpr T& get(tuple<Types...>& t) noexcept; template<class T, class... Types> constexpr T&& get(tuple<Types...>&& t) noexcept; template<class T, class... Types> constexpr const T& get(const tuple<Types...>& t) noexcept; template<class T, class... Types> constexpr const T&& get(const tuple<Types...>&& t) noexcept;
Mandates: The type T occurs exactly once in Types.
Returns: A reference to the element of t corresponding to the typeT in Types.
[Example 1: const tuple<int, const int, double, double> t(1, 2, 3.4, 5.6);const int& i1 = get(t); // OK, i1 has value 1const int& i2 = get(t); // OK, i2 has value 2const double& d = get(t); // error: type double is not unique within t â end example]
[Note 3:
The reason get is a non-member function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the template keyword.
â end note]
22.4.9 Relational operators [tuple.rel]
template<class... TTypes, class... UTypes> constexpr bool operator==(const tuple<TTypes...>& t, const tuple<UTypes...>& u); template<class... TTypes, [tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") UTuple> constexpr bool operator==(const tuple<TTypes...>& t, const UTuple& u);
For the first overload let UTuple be tuple<UTypes...>.
Constraints: For all i, where 0 ⤠i < sizeof...(TTypes),get(t) == get(u) is a valid expression anddecltype(get(t) == get(u)) models boolean-testable.
sizeof...(TTypes) equalstuple_size_v.
Returns: true if get(t) == get(u) for alli, otherwise false.
[Note 1:
If sizeof...(TTypes) equals zero, returns true.
â end note]
Remarks:
-
The elementary comparisons are performed in order from the zeroth index upwards. No comparisons or element accesses are performed after the first equality comparison that evaluates tofalse.
-
The second overload is to be found via argument-dependent lookup ([basic.lookup.argdep]) only.
template<class... TTypes, class... UTypes> constexpr common_comparison_category_t<synth-three-way-result<TTypes, UTypes>...> operator<=>(const tuple<TTypes...>& t, const tuple<UTypes...>& u); template<class... TTypes, [tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") UTuple> constexpr common_comparison_category_t<synth-three-way-result<TTypes, Elems>...> operator<=>(const tuple<TTypes...>& t, const UTuple& u);
For the second overload, Elems denotes the pack of typestuple_element_t<0, UTuple>,tuple_element_t<1, UTuple>, …,tuple_element_t<tuple_size_v - 1, UTuple>.
Effects: Performs a lexicographical comparison between t and u.
If sizeof...(TTypes) equals zero, returns strong_ordering::equal.
Otherwise, equivalent to:if (auto c = synth-three-way(get<0>(t), get<0>(u)); c != 0) return c;return ttail <=> utail; where rtail for some r is a tuple containing all but the first element of r.
Remarks: The second overload is to be found via argument-dependent lookup ([basic.lookup.argdep]) only.
[Note 2:
The above definition does not require ttail (or utail) to be constructed.
It might not even be possible, as t and u are not required to be copy constructible.
Also, all comparison operator functions are short circuited; they do not perform element accesses beyond what is needed to determine the result of the comparison.
â end note]
22.4.10 common_reference related specializations [tuple.common.ref]
In the descriptions that follow:
-
Let TTypes be a pack formed by the sequence of tuple_element_t<i, TTuple> for every integer 0â¤i<tuple_size_v.
-
Let UTypes be a pack formed by the sequence of tuple_element_t<i, UTuple> for every integer 0â¤i<tuple_size_v.
template<[tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") TTuple, [tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") UTuple, template<class> class TQual, template<class> class UQual> struct basic_common_reference<TTuple, UTuple, TQual, UQual> { using type = see below; };
Constraints:
-
TTuple is a specialization of tuple orUTuple is a specialization of tuple.
-
is_same_v<TTuple, decay_t> is true.
-
is_same_v<UTuple, decay_t> is true.
-
tuple_size_v equals tuple_size_v.
-
tuple<common_reference_t<TQual, UQual>...> denotes a type.
The member typedef-name type denotes the typetuple<common_reference_t<TQual, UQual>...>.
template<[tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") TTuple, [tuple-like](#concept:tuple-like "22.4.3 Concept tuple-like [tuple.like]") UTuple> struct common_type<TTuple, UTuple> { using type = see below; };
Constraints:
-
TTuple is a specialization of tuple orUTuple is a specialization of tuple.
-
is_same_v<TTuple, decay_t> is true.
-
is_same_v<UTuple, decay_t> is true.
-
tuple_size_v equals tuple_size_v.
-
tuple<common_type_t<TTypes, UTypes>...> denotes a type.
The member typedef-name type denotes the typetuple<common_type_t<TTypes, UTypes>...>.
22.4.11 Tuple traits [tuple.traits]
template<class... Types, class Alloc> struct uses_allocator<tuple<Types...>, Alloc> : true_type { };
Preconditions: Alloc meets the Cpp17Allocator requirements ([allocator.requirements.general]).
[Note 1:
Specialization of this trait informs other library components thattuple can be constructed with an allocator, even though it does not have a nested allocator_type.
â end note]
22.4.12 Tuple specialized algorithms [tuple.special]
template<class... Types> constexpr void swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(see below); template<class... Types> constexpr void swap(const tuple<Types...>& x, const tuple<Types...>& y) noexcept(see below);
Constraints:
-
For the first overload,(is_swappable_v && ...) is true.
-
For the second overload,(is_swappable_v && ...) is true.
Effects: As if by x.swap(y).
Remarks: The exception specification is equivalent to:noexcept(x.swap(y))