<tuple>

tuple

function template
<tuple>

std::swap (tuple)

template <class... Types>  void swap (tuple<Types...>& x, tuple<Types...>& y) noexcept ( /* see below */ );
Exchanges the contents of two tuples
The contents of the tuple object x are exchanged with those of y. Both objects must be of the same type (i.e., contain the same types of elements).

This is an overload of the generic swap that improves its performance by calling tuple::swap as in: x.swap(y).

Parameters

x,y
tuple objects (to the left- and right-hand side of the operator, respectively), containing both the same types of elements in the same order.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// swap tuples
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::get

int main ()
{
  std::tuple<int,char> a (10,'x');
  std::tuple<int,char> b (20,'y');

  swap(a,b);

  std::cout << "a contains: " << std::get<0>(a) << " and " << std::get<1>(a) << '\n';

  return 0;
}

Output:
a contains: 20 and y


Data races

The members of both pair objects, x and y, are modified.

Exception safety

If all the element types have a swap function defined and this is noexcept, this function never throws exceptions (no-throw guarantee).
Otherwise, if at least one of them swaps with move semantics, the operation may leave either or both tuple objects in an invalid state in case of exception (no guarantees).
Otherwise, the operation guarantees that both x and y end up in a valid state in case of exception (basic guarantee).

See also