std::basic_...::operator=(3)
π std::basic_...::operator=(3) π
okay bestie, this is the std::basic_...::operator=(3) page from the C++ Standard Libary manual β yes, that spelling is literally in the original, iykyk π. It's giving assignment-operator main character energy, and we are obsessed. π
β¨ NAME β¨
std::basic_string::operator= - std::basic_string::operator= π
like, that's the name, bestie. It's the assignment operator for std::basic_string, and it's here to replace what's inside your string. periodt.
πΈ Synopsis πΈ
okay so here's the whole overload family, and yes every single one is invited to the function party:
basic_string& operator=( const basic_string& str ); (1)
(constexpr since C++20)
basic_string& operator=( basic_string&& str ) (2) (since
C++11)
noexcept(/* see below */);
(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> (5) (since
C++11)
ilist );
(constexpr since C++20)
template<class StringViewLike> (6) (since
C++17)
basic_string& operator=( const StringViewLike& t );
(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*thisandstrare the same object, this function has no effect. - Replaces the contents with those of
strusingSequenceContainer's move assignment semantics. Unlike other sequence container move assignments, references, pointers, and iterators to elements ofstrmay be invalidated. β οΈ - Replaces the contents with those of the null-terminated character string pointed to by
sas if byassign(s, Traits::length(s)). - Replaces the contents with character
chas if byassign(std::addressof(ch), 1). - Replaces the contents with those of the initializer list
ilistas if byassign(ilist.begin(), ilist.size()). - Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then replaces the contents with those of thesvas if byassign(sv). This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true andstd::is_convertible_v<const StringViewLike&, const CharT*>is false. std::basic_stringcannot be assigned fromnullptr.
π§Έ 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_listto initialize the string with.tβ object convertible tostd::basic_string_viewto initialize the string with.
π«Ά Return value π«Ά
*this β she returns herself, as she should. π
β±οΈ Complexity β±οΈ
- Linear in size of
str. - Linear in the size of
*this(formally, eachCharThas to be destroyed). If allocators do not compare equal and do not propagate, then also linear in the size ofstr(copy must be made). - Linear in size of
s. - Constant.
- Linear in size of
ilist. - Linear in size of
t.
π Exceptions π
noexceptspecification:
noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || 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 (strong exception safety guarantee). πͺ
π Example π
// Run this code
#include <iomanip>
#include <iostream>
#include <string>
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.
- LWG 847 (C++98) β behavior as published: there was no exception safety guarantee; correct behavior: added strong exception safety guarantee.
- LWG 2063 (C++11) β behavior as published: the move assignment operator did not follow
SequenceContainer's semantic requirement; correct behavior: follows. - LWG 2946 (C++17) β behavior as published: overload (6) caused ambiguity in some cases; correct behavior: avoided by making it a template.
π See also π
- constructor β constructs a
basic_string(public member function). - assign β assign characters to a string (public member function).
operator=β assigns a view (public member function ofstd::basic_string_view<CharT,Traits>).
π·οΈ Category π·οΈ
- conditionally noexcept
mwah π this page is from http://cppreference.com, version/date 2024.06.10, and it's the std::basic_...::operator=(3) manual. go assign something cute, bestie. π¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms