std::basic_string<CharT,Traits,Allocator>::operator+=
: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (1) | ||
basic_string& operator+=( const basic_string& str ); |
(C++20) | |
constexpr basic_string& operator+=( const basic_string& str ); |
(C++20) | |
| (2) | ||
basic_string& operator+=( CharT ch ); |
(C++20) | |
constexpr basic_string& operator+=( CharT ch ); |
(C++20) | |
| (3) | ||
basic_string& operator+=( const CharT* s ); |
(C++20) | |
constexpr basic_string& operator+=( const CharT* s ); |
(C++20) | |
| (4) | ||
basic_string& operator+=( std::initializer_list<CharT> ilist ); |
(C++11) (C++20) |
|
constexpr basic_string& operator+=( std::initializer_list<CharT> ilist ); |
(C++20) | |
| (5) | ||
template < class T > basic_string& operator+=( const T& t ); |
(C++17) (C++20) |
|
template < class T > constexpr basic_string& operator+=( const T& t ); |
(C++20) | |
1)
str 2)
ch 3)
s 4)
ilist 5)
std::basic_string_view<CharT, Traits> sv = t; t sv append(sv) sv std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const T&, const CharT*> false | str | - | |
| ch | - | |
| s | - | |
| ilist | - | std::initializer_list |
| t | - | (std::basic_string_view ) |
*this
() (C++11)
size() > max_size() std::length_error
(2) CharT CharT char std::string
C++
| DR | |||
|---|---|---|---|
| LWG 2946 | C++17 | string_view overload causes ambiguity in some cases
|
avoided by making it a template |
Run this code
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
std::string str;
str.reserve(50); //reserves sufficient storage space to avoid memory reallocation
std::cout << std::quoted(str) << '\n'; //empty string
str += "This";
std::cout << std::quoted(str) << '\n';
str += std::string(" is ");
std::cout << std::quoted(str) << '\n';
str += 'a';
std::cout << std::quoted(str) << '\n';
str += {' ','s','t','r','i','n','g','.'};
std::cout << std::quoted(str) << '\n';
str += 76.85; // equivalent to str += static_cast<char>(76.85), might not be the intent
std::cout << std::quoted(str) << '\n';
}
:
""
"This"
"This is "
"This is a"
"This is a string."
"This is a string.L"
| () |