std::basic_string<CharT,Traits,Allocator>::operator=
| (1) | ||
| basic_string& operator=( const basic_string& str ); |
(until C++20) | |
| constexpr basic_string& operator=( const basic_string& str ); |
(since C++20) | |
| (2) | ||
| basic_string& operator=( basic_string&& str ); |
(since C++11) (until C++17) |
|
| basic_string& operator=( basic_string&& str ) noexcept(/* see below */); |
(since C++17) (until C++20) |
|
| constexpr basic_string& operator=( basic_string&& str ) noexcept(/* see below */); |
(since C++20) | |
| (3) | ||
| basic_string& operator=( const CharT* s ); |
(until C++20) | |
| constexpr basic_string& operator=( const CharT* s ); |
(since C++20) | |
| (4) | ||
| basic_string& operator=( CharT ch ); |
(until C++20) | |
| constexpr basic_string& operator=( CharT ch ); |
(since C++20) | |
| (5) | ||
| basic_string& operator=( std::initializer_list<CharT> ilist ); |
(since C++11) (until C++20) |
|
| constexpr basic_string& operator=( std::initializer_list<CharT> ilist ); |
(since C++20) | |
| (6) | ||
| template<class T> basic_string& operator=( const T& t ); |
(since C++17) (until C++20) |
|
| template<class T> constexpr basic_string& operator=( const T& t ); |
(since C++20) | |
Replaces the contents of the string.
str. If *this and str are the same object, this function has no effect.str using move semantics. str is in a valid but unspecified state afterwards. If std::allocator_traits<Allocator>::propagate_on_container_move_assignment() is true, the target allocator is replaced by a copy of the source allocator. If it is false and the source and the target allocators do not compare equal, the target cannot take ownership of the source memory 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.s as if by assign(s, Traits::length(s)).ilist as if by assign(ilist.begin(), ilist.size())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 only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.Contents |
[edit] 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 |
[edit] Return value
*this
[edit] Complexity
strthis (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)silist[edit] Exceptions
|
2)
noexcept specification:
noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value) |
(since C++17) |
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 (strong exception guarantee). |
(since C++11) |
[edit] 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 valid implementation of move assignment | corrected to support allocators |
| LWG 2946 | C++17 | string_view overload causes ambiguity in some cases
|
avoided by making it a template |
[edit] Example
| This section is incomplete Reason: no example |
[edit] See also
constructs a basic_string (public member function) | |
| assign characters to a string (public member function) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
