public member function

std::forward_list::operator=

<forward_list>
copy (1)
forward_list& operator= (const forward_list& fwdlst);
move (2)
forward_list& operator= (forward_list&& fwdlst);
initializer list(3)
forward_list& operator= (initializer_list<value_type> il);
Assign content
Assigns fwdlst (or il) as the new content for the container.

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

The copying assignment (1) copies all the elements in fwdlst into the container object (with fwdlst preserving its contents).
The move assignment (2) transfer ownership of the contents of fwdlst to the object without performing any copies (fwdlst loses its content and becomes empty).

Parameters

fwdlst
A forward_list object of the same type (i.e., with the same template parameters).
il
An initializer_list object. The compiler will automatically construct such objects from initializer list declarators.
Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).

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
25
26
27
// assignment operator with forward_list
#include <iostream>
#include <forward_list>

template<class Container>
Container by_two (const Container& x) {
  Container temp(x); for (auto& x:temp) x*=2; return temp;
}

int main ()
{
  std::forward_list<int> first (4);      // 4 ints
  std::forward_list<int> second (3,5);   // 3 ints with value 5

  first = second;                        // copy assignment
  second = by_two(first);                // move assignment

  std::cout << "first: ";
  for (int& x : first) std::cout << " " << x;
  std::cout << std::endl;

  std::cout << "second: ";
  for (int& x : second) std::cout << " " << x;
  std::cout << std::endl;

  return 0;
}

In the first assignment, second is an lvalue: the copy assignment function is called.
In the second assignment, the value returned by by_two(first) is an rvalue: the move assignment function is called.
Output:
first: 5 5 5
second: 10 10 10

Complexity

For the copy assignment: Linear in sizes before and after the operation (destructions, copy constructions).
For the move assignment: Linear in current container size (destructions).

Iterator validity

All iterators, references and pointers to elements that were in the container before the call are invalidated.

See also