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.

Any elements contained in the object before the call are removed form the container and destroyed. They are replaced by those in fwdlst (or il), if any.

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

The container preserves its current allocator, except if the allocator traits of fwdlst's allocator type indicates it should propagate.

Parameters

fwdlst
A forward_list object of the same type (i.e., with the same template parameters, T and Alloc).
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 << '\n';

  std::cout << "second: ";
  for (int& x : second) std::cout << ' ' << x;
  std::cout << '\n';

  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 related to this container are invalidated, except the end iterators.

In the move assignment, iterators, pointers and references referring to elements in fwdlist remain valid, keep referring to the moved elements in their new container, and iterators now iterate in it.

Data races

All copied elements are accessed.
The move assignment (2) modifies fwdlst.
The container and all its elements are modified.

Exception safety

Basic guarantee: if an exception is thrown, the container is in a valid state.
It throws if the constructor of any element throws or if the internal allocator object throws attempting to allocate storage.

See also