Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | basic string
(+)
m
Line 19: Line 19:
 
{{dcl | num=6 | since=c++17 | 1=
 
{{dcl | num=6 | since=c++17 | 1=
 
template<class T>
 
template<class T>
basic_string& operator=( const T& t);
+
basic_string& operator=( const T& t );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}

Revision as of 08:08, 18 March 2018

 
 
 
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)
basic_string& operator=( basic_string&& str );
(2) (since C++11)
basic_string& operator=( const CharT* s );
(3)
basic_string& operator=( CharT ch );
(4)
basic_string& operator=( std::initializer_list<CharT> ilist );
(5) (since C++11)
template<class T>
basic_string& operator=( const T& t );
(6) (since C++17)

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. Leaves str in valid, but unspecified state. If *this and str are the same object, the function has no effect.
(until C++17)
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() 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.
(since C++17)
3) Replaces the contents with those of null-terminated character string pointed to by s as if by *this = basic_string(s), which involves a call to Traits::length(s).
4) Replaces the contents with character ch as if by *this = basic_string(1,c)
5) Replaces the contents with those of the initializer list ilist as if by *this = basic_string(ilist)
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.

Contents

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
init - 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 init

Exceptions

2)
noexcept specification:  
noexcept
  
(since C++11)
(until C++17)
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)

Example

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

See also

constructs a basic_string
(public member function) [edit]
assign characters to a string
(public member function) [edit]