std::basic_string::operator=(3)

NAME
std::basic_string::operator= - std::basic_string::operator=

Synopsis
   basic_string& operator=( (until
   const basic_string& str ); C++20)
   constexpr basic_string& (since
   operator=( const C++20)
   basic_string& str );
                        (since
   basic_string& operator=( C++11)
   basic_string&& str ); (until
                        C++17)
   basic_string& operator=( (since
   basic_string&& str ) C++17)
   noexcept(/* see below */); (until
                        C++20)
   constexpr basic_string& (since
   operator=( basic_string&& str C++20)
   ) noexcept(/* see below */);
   basic_string& operator=( (until
   const CharT* s ); C++20)
   constexpr basic_string& (since
   operator=( const CharT* s ); C++20)
   basic_string& operator=( (until
   CharT ch ); (1) C++20)
   constexpr basic_string& (since
   operator=( CharT ch ); C++20)
   basic_string& operator=( (2) (since
   std::initializer_list<CharT> C++11)
   ilist ); (until
                        C++20)
   constexpr basic_string&
   operator=( (3) (since
   std::initializer_list<CharT> C++20)
   ilist ); (4)
   template<class (since
   StringViewLike> (5) C++17)
   basic_string& operator=( (until
   const StringViewLike& t ); C++20)
   template<class
   StringViewLike> (6) (since
   constexpr basic_string& C++20)
   operator=( const
   StringViewLike& t );
   constexpr basic_string& (since
   operator=( std::nullptr_t ) = (7) C++23)
   delete;
   Replaces the contents of the string.
   1) Replaces the contents with a copy of str. If *this and str are the same object,
   this function has no effect.
   2) Replaces the contents with those of str using move semantics. str is in a valid
   but unspecified state afterwards. If
   std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value is
   true, the allocator of *this is replaced by a copy of that of str. If it is false
   and the allocators of *this and str do not compare equal, *this cannot take
   ownership of the memory owned by str and must assign each character individually,
   allocating additional memory using its own allocator as needed. Unlike other
   container move assignments, references, pointers, and iterators to str may be
   invalidated.
   3) Replaces the contents with those of null-terminated character string pointed to
   by s as if by assign(s, Traits::length(s)).
   4) Replaces the contents with character ch as if by assign(std::addressof(ch), 1)
   5) Replaces the contents with those of the initializer list ilist as if by
   assign(ilist.begin(), ilist.size())
   6) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT,
   Traits> sv = t;, then replaces the contents with those of the sv as if by
   assign(sv). This overload participates in overload resolution only if
   std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
   is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.
   7) basic_string cannot be assigned from nullptr.

Parameters
   ch - value to initialize characters of the string with
   str - string to be used as source to initialize the string with
   s - pointer to a null-terminated character string to use as source to initialize
           the string with
   ilist - std::initializer_list to initialize the string with
   t - object convertible to std::basic_string_view to initialize the string with

Return value
   *this

Complexity
   1) linear in size of str.
   2) linear in the size of *this (formally, each CharT has to be destroyed). If
   allocators do not compare equal and do not propagate, then also linear in the size
   of str (copy must be made).
   3) linear in size of s.
   4) constant.
   5) linear in size of ilist.

Exceptions
2) noexcept specification: (since noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value C++17) || std::allocator_traits<Allocator>::is_always_equal::value)
   If the operation would result in size() > max_size(), throws std::length_error.
   If an exception is thrown for any reason, this function has no effect (since C++11)
   (strong exception guarantee).

Example
// Run this code
 #include <string>
 #include <iostream>
 #include <iomanip>
 int main()
 {
     std::string str1;
     std::string str2 { "alpha" };
     // (1) operator=( const basic_string& );
     str1 = str2;
     std::cout << std::quoted(str1) << ' ' // "alpha"
               << std::quoted(str2) << '\n'; // "alpha"
     // (2) operator=( basic_string&& );
     str1 = std::move(str2);
     std::cout << std::quoted(str1) << ' ' // "alpha"
               << std::quoted(str2) << '\n'; // "" or "alpha" (unspecified)
     // (3) operator=( const CharT* );
     str1 = "beta";
     std::cout << std::quoted(str1) << '\n'; // "beta"
     // (4) operator=( CharT );
     str1 = '!';
     std::cout << std::quoted(str1) << '\n'; // "!"
     // (5) operator=( std::initializer_list<CharT> );
     str1 = {'g','a','m','m','a'};
     std::cout << std::quoted(str1) << '\n'; // "gamma"
     // (6) operator=( const T& );
     str1 = 35U; // equivalent to str1 = static_cast<char>(35U);
     std::cout << std::quoted(str1) << '\n'; // "#" (ASCII = 35)
 }

Possible output:
 "alpha" "alpha"
 "alpha" ""
 "beta"
 "!"
 "gamma"
 "#"
  Defect reports
   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.
      DR Applied to Behavior as published Correct behavior
   LWG 2063 C++11 non-normative note stated that swap is a corrected to support
                       valid implementation of move assignment allocators
   LWG 2946 C++17 string_view overload causes ambiguity in avoided by making it a
                       some cases template

See also
   constructor constructs a basic_string
                 (public member function)
   assign assign characters to a string
                 (public member function)
   operator= assigns a view
   (C++17) (public member function of std::basic_string_view<CharT,Traits>)