public member function
<forward_list>

std::forward_list::operator=

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 new contents to the container, replacing its current contents.

The copy assignment (1) copies all the elements from fwdlst into the container (with fwdlst preserving its contents).

The move assignment (2) moves the elements of fwdlst into the container (x is left in an unspecified but valid state).

The initializer list assignment (3) copies the elements of il into the container.

The container preserves its current allocator, except if the allocator traits indicate fwdlst's allocator should propagate. This allocator is used (through its traits) to allocate or deallocate if there are changes in storage requirements, and to construct or destroy elements, if needed.

Any elements held in the container before the call are either assigned to or destroyed.

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

Linear in the number of elements.

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 x are also invalidated.

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.
If allocator_traits::construct is not supported with the appropriate arguments for the element constructions, or if value_type is not copy assignable (or move assignable for (2)), it causes undefined behavior.

See also