std::basic_string<CharT,Traits,Allocator>::operator= - cppreference.com (original) (raw)
| basic_string& operator=( const basic_string& str ); | (1) | (constexpr since C++20) |
|---|---|---|
| basic_string& operator=( basic_string&& str ) noexcept(/* see below */); | (2) | (since C++11) (constexpr since C++20) |
| basic_string& operator=( const CharT* s ); | (3) | (constexpr since C++20) |
| basic_string& operator=( CharT ch ); | (4) | (constexpr since C++20) |
| basic_string& operator=( std::initializer_list<CharT> ilist ); | (5) | (since C++11) (constexpr since C++20) |
| template<class StringViewLike>basic_string& operator=( const StringViewLike& t ); | (6) | (since C++17) (constexpr since C++20) |
| basic_string& operator=( std::nullptr_t ) = delete; | (7) | (since C++23) |
Replaces the contents of the string.
Replaces the contents with a copy of str. If *this and str are the same object, this function has no effect.
Replaces the contents with those of str using SequenceContainer's move assignment semantics.
Unlike other sequence container move assignments, references, pointers, and iterators to elements of str may be invalidated.
Replaces the contents with those of null-terminated character string pointed to by s as if by assign(s, Traits::length(s)).
Replaces the contents with character ch as if by assign(std::addressof(ch), 1).
Replaces the contents with those of the initializer list ilist as if by assign(ilist.begin(), ilist.size()).
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).
std::basic_stringcannot be assigned from nullptr.
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
Linear in size of str.
Linear in the size of *this (formally, each
CharThas 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).Linear in size of s.
Constant.
Linear in size of ilist.
Linear in size of t.
[edit] Exceptions
If the operation would cause size() to exceed max_size(), throws std::length_error.
If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).
[edit] Example
#include #include #include 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); str1 = {'g', 'a', 'm', 'm', 'a'}; std::cout << std::quoted(str1) << '\n'; // "gamma" // (6) operator=(const T&); str1 = 35U; // equivalent to str1 = static_cast(35U); std::cout << std::quoted(str1) << '\n'; // "#" (ASCII = 35) }
Possible output:
"alpha" "alpha" "alpha" "" "beta" "!" "gamma" "#"
[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 847 | C++98 | there was no exception safety guarantee | added strong exception safety guarantee |
| LWG 2063 | C++11 | the move assignment operator did not followSequenceContainer's semantic requirement | follows |
| LWG 2946 | C++17 | overload (6) caused ambiguity in some cases | avoided by making it a template |
[edit] See also
| | constructs a basic_string (public member function) [edit] | | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | assign characters to a string (public member function) [edit] | | | assigns a view (public member function of std::basic_string_view<CharT,Traits>) [edit] |