Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/operator="

From cppreference.com
< cpp‎ | string‎ | basic string
m (Slight rewording for POCMA.)
m (Synopsis: "notes=" ⇒ "constexpr=")
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{cpp/string/basic_string/title | operator{{=}}}}
+
{{cpp/string/basic_string/title|operator{{=}}}}
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl rev multi | num=1
+
{{dcla|anchor=no|num=1|constexpr=c++20|1=
| dcl1=
+
 
basic_string& operator=( const basic_string& str );
 
basic_string& operator=( const basic_string& str );
| since2=c++20 | dcl2=
 
constexpr basic_string& operator=( const basic_string& str );
 
 
}}
 
}}
{{dcl rev multi | num=2
+
{{dcla|anchor=no|num=2|since=c++11|constexpr=c++20|1=
| since1=c++11 | dcl1=
+
basic_string& operator=( basic_string&& str )
basic_string& operator=( basic_string&& str );
+
     noexcept(/* see below */);
| since2=c++17 | dcl2=
+
basic_string& operator=( basic_string&& str ) noexcept(/* see below */);
+
| since3=c++20 | dcl3=
+
constexpr basic_string&
+
     operator=( basic_string&& str ) noexcept(/* see below */);
+
 
}}
 
}}
{{dcl rev multi | num=3
+
{{dcla|anchor=no|num=3|constexpr=c++20|1=
| dcl1=
+
 
basic_string& operator=( const CharT* s );
 
basic_string& operator=( const CharT* s );
| since2=c++20 | dcl2=
 
constexpr basic_string& operator=( const CharT* s );
 
 
}}
 
}}
{{dcl rev multi | num=4
+
{{dcla|anchor=no|num=4|constexpr=c++20|1=
| dcl1=
+
 
basic_string& operator=( CharT ch );
 
basic_string& operator=( CharT ch );
| since2=c++20 | dcl2=
 
constexpr basic_string& operator=( CharT ch );
 
 
}}
 
}}
{{dcl rev multi | num=5
+
{{dcla|anchor=no|num=5|since=c++11|constexpr=c++20|1=
| since1=c++11 | dcl1=
+
 
basic_string& operator=( std::initializer_list<CharT> ilist );
 
basic_string& operator=( std::initializer_list<CharT> ilist );
| since2=c++20 | dcl2=
 
constexpr basic_string& operator=( std::initializer_list<CharT> ilist );
 
 
}}
 
}}
{{dcl rev multi | num=6
+
{{dcla|num=6|since=c++17|constexpr=c++20|1=
| since1=c++17 | dcl1=
+
template<class StringViewLike>
template<class T>
+
basic_string& operator=( const StringViewLike& t );
basic_string& operator=( const T& t );
+
}}
| since2=c++20 | dcl2=
+
{{dcl|num=7|since=c++23|1=
template<class T>
+
basic_string& operator=( std::nullptr_t ) = delete;
constexpr basic_string& operator=( const T& t );
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 47: Line 29:
 
Replaces the contents of the string.
 
Replaces the contents of the string.
  
@1@ Replaces the contents with a copy of {{tt|str}}. If {{c|*this}} and {{tt|str}} are the same object, this function has no effect.
+
@1@ Replaces the contents with a copy of {{c|str}}. If {{c|*this}} and {{c|str}} are the same object, this function has no effect.
  
@2@ Replaces the contents with those of {{tt|str}} using move semantics. {{tt|str}} is in a valid but unspecified state afterwards. If {{c|std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value}} is {{c|true}}, the target allocator is replaced by a copy of the source allocator. If it is {{c|false}} and the allocators of {{c|*this}} and {{tt|str}} do not compare equal, {{c|*this}} cannot take ownership of the memory owned by {{tt|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 {{tt|str}} may be invalidated.
+
@2@ Replaces the contents with those of {{c|str}} using {{named req|SequenceContainer}}'s move assignment semantics.
@3@ Replaces the contents with those of null-terminated character string pointed to by {{tt|s}} as if by {{c|assign(s, Traits::length(s))}}.
+
@@ Unlike other sequence container move assignments, references, pointers, and iterators to elements of {{c|str}} may be invalidated.
@4@ Replaces the contents with character {{tt|ch}} as if by {{c|assign(std::addressof(ch), 1)}}
+
@3@ Replaces the contents with those of null-terminated character string pointed to by {{c|s}} as if by {{c|assign(s, Traits::length(s))}}.
@5@ Replaces the contents with those of the initializer list {{tt|ilist}} as if by {{c|assign(ilist.begin(), ilist.size())}}
+
@4@ Replaces the contents with character {{c|ch}} as if by {{c|assign(std::addressof(ch), 1)}}.
@6@ {{cpp/string/sv hack|replaces the contents with those of the {{tt|sv}} as if by {{c|assign(sv)}}}}
+
@5@ Replaces the contents with those of the initializer list {{c|ilist}} as if by {{c|assign(ilist.begin(), ilist.size())}}.
 +
@6@ {{cpp/string/sv hack|replaces the contents with those of the {{c|sv}} as if by {{c|assign(sv)}}}}
 +
@7@ {{tt|std::basic_string}} cannot be assigned from {{c|nullptr}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | ch | value to initialize characters of the string with}}
+
{{par|ch|value to initialize characters of the string with}}
{{par | str | string to be used as source to initialize the string with}}
+
{{par|str|string to be used as source to initialize the string with}}
{{par | s | pointer to a null-terminated character string to use as source to initialize the string with}}
+
{{par|s|pointer to a null-terminated character string to use as source to initialize the string with}}
{{par | ilist | {{lc|std::initializer_list}} to initialize the string with}}
+
{{par|ilist|{{lc|std::initializer_list}} to initialize the string with}}
{{par | t | object convertible to {{lc|std::basic_string_view}} to initialize the string with}}
+
{{par|t|object convertible to {{lc|std::basic_string_view}} to initialize the string with}}
 
{{par end}}  
 
{{par end}}  
  
Line 68: Line 52:
  
 
===Complexity===
 
===Complexity===
@1@ linear in size of {{tt|str}}.
+
@1@ Linear in size of {{c|str}}.
  
@2@ linear in the size of {{c|*this}} (formally, each {{tt|CharT}} has to be destroyed). If allocators do not compare equal and do not propagate, then also linear in the size of {{tt|str}} (copy must be made).
+
@2@ Linear in the size of {{c|*this}} (formally, each {{tt|CharT}} has to be destroyed). If allocators do not compare equal and do not propagate, then also linear in the size of {{c|str}} (copy must be made).
  
@3@ linear in size of {{tt|s}}.
+
@3@ Linear in size of {{c|s}}.
  
@4@ constant.
+
@4@ Constant.
  
@5@ linear in size of {{tt|ilist}}.
+
@5@ Linear in size of {{c|ilist}}.
 +
 
 +
@6@ Linear in size of {{c|t}}.
  
 
===Exceptions===
 
===Exceptions===
{{rrev|since=c++17|
+
@2@ {{noexcept|std::allocator_traits<Allocator>::
@2@ {{noexcept|std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value
+
            propagate_on_container_move_assignment::value {{!!}}
{{!!}} std::allocator_traits<Allocator>::is_always_equal::value}}
+
        std::allocator_traits<Allocator>::is_always_equal::value}}
}}
+
  
If the operation would result in {{c|size() > max_size()}}, throws {{lc|std::length_error}}.
+
{{cpp/string/basic_string/length error note}}
  
{{rrev|since=c++11|
+
{{cpp/strong exception safety guarantee}}
If an exception is thrown for any reason, this function has no effect (strong exception guarantee).}}
+
  
 
===Example===
 
===Example===
{{example |
+
{{example|
| code=
+
|code=
#include <string>
+
#include <iostream>
+
 
#include <iomanip>
 
#include <iomanip>
 +
#include <iostream>
 +
#include <string>
  
 
int main()
 
int main()
 
{
 
{
 
     std::string str1;
 
     std::string str1;
     std::string str2 { "alpha" };
+
     std::string str2{"alpha"};
 
+
   
     // (1) operator=( const basic_string& );
+
     // (1) operator=(const basic_string&);
 
     str1 = str2;
 
     str1 = str2;
     std::cout << std::quoted(str1) << ' ' // "alpha"
+
     std::cout << std::quoted(str1) << ' '   // "alpha"
 
               << std::quoted(str2) << '\n'; // "alpha"
 
               << std::quoted(str2) << '\n'; // "alpha"
 
+
   
     // (2) operator=( basic_string&& );
+
     // (2) operator=(basic_string&&);
 
     str1 = std::move(str2);
 
     str1 = std::move(str2);
     std::cout << std::quoted(str1) << ' ' // "alpha"
+
     std::cout << std::quoted(str1) << ' '   // "alpha"
 
               << std::quoted(str2) << '\n'; // "" or "alpha" (unspecified)
 
               << std::quoted(str2) << '\n'; // "" or "alpha" (unspecified)
 
      
 
      
     // (3) operator=( const CharT* );
+
     // (3) operator=(const CharT*);
 
     str1 = "beta";
 
     str1 = "beta";
 
     std::cout << std::quoted(str1) << '\n'; // "beta"
 
     std::cout << std::quoted(str1) << '\n'; // "beta"
 
+
   
     // (4) operator=( CharT );
+
     // (4) operator=(CharT);
 
     str1 = '!';  
 
     str1 = '!';  
 
     std::cout << std::quoted(str1) << '\n'; // "!"
 
     std::cout << std::quoted(str1) << '\n'; // "!"
 
+
   
     // (5) operator=( std::initializer_list<CharT> );
+
     // (5) operator=(std::initializer_list<CharT>);
     str1 = {'g','a','m','m','a'};
+
     str1 = {'g', 'a', 'm', 'm', 'a'};
 
     std::cout << std::quoted(str1) << '\n'; // "gamma"
 
     std::cout << std::quoted(str1) << '\n'; // "gamma"
 
+
   
     // (6) operator=( const T& );
+
     // (6) operator=(const T&);
 
     str1 = 35U; // equivalent to str1 = static_cast<char>(35U);
 
     str1 = 35U; // equivalent to str1 = static_cast<char>(35U);
 
     std::cout << std::quoted(str1) << '\n'; // "#" (ASCII = 35)
 
     std::cout << std::quoted(str1) << '\n'; // "#" (ASCII = 35)
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
 
"alpha" "alpha"
 
"alpha" "alpha"
 
"alpha" ""
 
"alpha" ""
Line 139: Line 123:
 
===Defect reports===
 
===Defect reports===
 
{{dr list begin}}
 
{{dr list begin}}
{{dr list item|wg=lwg|dr=2063|std=C++11|before=non-normative note stated that swap is a valid implementation of move assignment|after=corrected to support allocators}}
+
{{dr list item|wg=lwg|dr=847|std=C++98|before=there was no exception safety guarantee|after=added strong exception safety guarantee}}
{{dr list item|wg=lwg|dr=2946|std=C++17|before={{tt|string_view}} overload causes ambiguity in some cases|after=avoided by making it a template}}
+
{{dr list item|wg=lwg|dr=2063|std=C++11|before=the move assignment operator did not follow<br>{{named req|SequenceContainer}}'s semantic requirement|after=follows}}
 +
{{dr list item|wg=lwg|dr=2946|std=C++17|before=overload {{vl|6}} caused ambiguity in some cases|after=avoided by making it a template}}
 
{{dr list end}}
 
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/basic_string/dsc constructor}}
+
{{dsc inc|cpp/string/basic_string/dsc constructor}}
{{dsc inc | cpp/string/basic_string/dsc assign}}
+
{{dsc inc|cpp/string/basic_string/dsc assign}}
{{dsc inc | cpp/string/basic_string_view/dsc operator{{=}} }}
+
{{dsc inc|cpp/string/basic_string_view/dsc operator{{=}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 00:04, 25 May 2024

 
 
 
std::basic_string
Member functions
basic_string::operator=
Element access
Iterators
Capacity
Modifiers
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
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.

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 SequenceContainer's move assignment semantics.
Unlike other sequence container move assignments, references, pointers, and iterators to elements of 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) std::basic_string cannot 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

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.
6) Linear in size of t.

[edit] Exceptions

2)
noexcept specification:  
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).

[edit] Example

#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"
"#"

[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 follow
SequenceContainer'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]