public member function

std::tuple::operator=

<tuple>
copy (1)
tuple& operator= (const tuple& tpl);
move (2)
tuple& operator= (tuple&& tpl) noexcept( /* see below */ );
implicit conversion (3)
template <class... UTypes>
  tuple& operator= (const tuple<UTypes...>& tpl);
implicit conversion / move (4)
template <class... UTypes>
  tuple& operator= (tuple<UTypes...>&& tpl);
conversion from pair (5)
template <class U1, class U2>
  tuple& operator= (const pair<U1,U2>& pr);
from pair / move (6)
template <class U1, class U2>
  tuple& operator= (pair<U1,U2>&& pr);
Assign content
Assigns tpl (or pr) as the new content for the tuple object.

The elements contained in the object before the call are destroyed, and replaced by those in tpl or pr, if any.

The first version (1) performs a copy assignment, which copies all the elements in tpl into the container object (with tpl preserving its contents). While the second version (2) performs a move assignment, which transfer ownership of the contents of tpl to the object.

This second version (2) is only noexcept if each of the types in the tuple have noexcept move assignments.

In the first case, both the tuple object and tpl end up with their own copy of the same contents. In the second case, no copies occur, and the content is lost by tpl.

The same applies as well to the converting versions: if the arguments are rvalues of moveable types, the even-numbered versions ((2), (4) or (6)) are called and the elements are moved instead of copied.

Parameters

tpl
Another tuple object with the same number of elements.
This may be an object of the same type as the object being constructed (version 2) or of a tuple type whose elements' types are implicitly convertible to those in the tuple being constructed.
pr
A pair object whose first and second types (U1 and U2) match those used as class template arguments, or are implicitly convertible to them.

Return value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// tuple assignments
#include <iostream>
#include <utility>
#include <tuple>

int main ()
{
  std::pair<int,char> mypair (0,' ');

  std::tuple<int,char> a (10,'x');
  std::tuple<long,char> b, c;

  b = a;                                // copy assignment
  c = std::make_tuple (100L,'Y');       // move assignment
  a = c;                                // conversion assignment
  c = std::make_tuple (100,'z');        // conversion / move assignment
  a = mypair;                           // from pair assignment
  a = std::make_pair (2,'b');           // form pair /move assignment

  std::cout << "c contains: " << std::get<0>(c);
  std::cout << " and " << std::get<1>(c) << std::endl;

  return 0;
}

Output:
c contains: 100 and z

See also

tuple::make_tuple