| Defined in header <string>
|
||
template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( const std::basic_string<CharT,Traits,Alloc>& lhs, const std::basic_string<CharT,Traits,Alloc>& rhs ); |
(1) | (constexpr since C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( const std::basic_string<CharT,Traits,Alloc>& lhs, const CharT* rhs ); |
(2) | (constexpr since C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( const std::basic_string<CharT,Traits,Alloc>& lhs, CharT rhs ); |
(3) | (constexpr since C++20) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> operator+( const std::basic_string<CharT,Traits,Alloc>& lhs, std::type_identity_t<std::basic_string_view<CharT,Traits>> rhs ); |
(4) | (since C++26) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( const CharT* lhs, const std::basic_string<CharT,Traits,Alloc>& rhs ); |
(5) | (constexpr since C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( CharT lhs, const std::basic_string<CharT,Traits,Alloc>& rhs ); |
(6) | (constexpr since C++20) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> operator+( std::type_identity_t<std::basic_string_view<CharT,Traits>> lhs, const std::basic_string<CharT,Traits,Alloc>& rhs ); |
(7) | (since C++26) |
template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( std::basic_string<CharT,Traits,Alloc>&& lhs, std::basic_string<CharT,Traits,Alloc>&& rhs ); |
(8) | (since C++11) (constexpr since C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( std::basic_string<CharT,Traits,Alloc>&& lhs, const std::basic_string<CharT,Traits,Alloc>& rhs ); |
(9) | (since C++11) (constexpr since C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( std::basic_string<CharT,Traits,Alloc>&& lhs, const CharT* rhs ); |
(10) | (since C++11) (constexpr since C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( std::basic_string<CharT,Traits,Alloc>&& lhs, CharT rhs ); |
(11) | (since C++11) (constexpr since C++20) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> operator+( std::basic_string<CharT,Traits,Alloc>&& lhs, std::type_identity_t<std::basic_string_view<CharT,Traits>> rhs ); |
(12) | (since C++26) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( const std::basic_string<CharT,Traits,Alloc>& lhs, std::basic_string<CharT,Traits,Alloc>&& rhs ); |
(13) | (since C++11) (constexpr since C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( const CharT* lhs, std::basic_string<CharT,Traits,Alloc>&& rhs ); |
(14) | (since C++11) (constexpr since C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> operator+( CharT lhs, std::basic_string<CharT,Traits,Alloc>&& rhs ); |
(15) | (since C++11) (constexpr since C++20) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> operator+( std::type_identity_t<std::basic_string_view<CharT,Traits>> lhs, std::basic_string<CharT,Traits,Alloc>&& rhs ); |
(16) | (since C++26) |
Returns a string containing characters from lhs followed by the characters from rhs. Equivalent to:
std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;std::basic_string<CharT, Traits, Allocator> r = lhs; r.push_back(rhs); return r;std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(r.begin(), lhs); return r;std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;lhs.append(rhs); return std::move(lhs); except that both lhs and rhs are left in valid but unspecified states. If lhs and rhs have equal allocators, the implementation can move from either.lhs.append(rhs); return std::move(lhs);lhs.push_back(rhs); return std::move(lhs);lhs.append(rhs); return std::move(lhs);rhs.insert(0, lhs); return std::move(rhs);rhs.insert(rhs.begin(), lhs); return std::move(rhs);rhs.insert(0, lhs); return std::move(rhs);|
The allocator used for the result is: 1-4)
std::allocator_traits<Alloc>::select_on_container_copy_construction(lhs.get_allocator())5-7)
std::allocator_traits<Alloc>::select_on_container_copy_construction(rhs.get_allocator())8-12)
lhs.get_allocator()13-16)
rhs.get_allocator()In other words:
In each case, the left operand is preferred when both are For (8-16), all rvalue |
(since C++11) |
Parameters
| lhs | - | string, string view(since C++26), character, or pointer to the first character in a null-terminated array |
| rhs | - | string, string view(since C++26), character, or pointer to the first character in a null-terminated array |
Return value
A string containing characters from lhs followed by the characters from rhs, using the allocator determined as above(since C++11).
Notes
Because the allocator used by the result of For a chain of For better and portable control over allocators, member functions like |
(since C++11) |
|
The usage of std::type_identity_t as parameter in overloads (4), (7), (12), and (16) ensures that an object of type std::basic_string<CharT, Traits, Allocator> can always be concatenated to an object of a type
|
(since C++26) |
Example
#include <iostream>
#include <string>
#include <string_view>
int main()
{
std::string s1 = "Hello";
std::string s2 = "world";
const char* end = "!\n";
std::cout << s1 + ' ' + s2 + end;
std::string_view water{" Water"};
#if __cpp_lib_string_view >= 202403
std::cout << s1 + water + s2 << end; // overload (4), then (1)
#else
std::cout << s1 + std::string(water) + s2 << end; // OK, but less efficient
#endif
}Hello world!
Hello Waterworld!Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P1165R1 | C++11 | allocator propagation is haphazard and inconsistent | made more consistent |
See also
| appends characters to the end (public member function) | |
| appends characters to the end (public member function) | |
| inserts characters (public member function) |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.