std::operator+(std::basic_string)
| 헤더 <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) | (C++20부터 constexpr) |
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) | (C++20부터 constexpr) |
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) | (C++20부터 constexpr) |
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) | (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) | (C++20부터 constexpr) |
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) | (C++20부터 constexpr) |
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) | (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) | (C++11부터) (C++20부터 constexpr) |
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) | (C++11부터) (C++20부터 constexpr) |
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) | (C++11부터) (C++20부터 constexpr) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
CharT rhs );
|
(11) | (C++11부터) (C++20부터 constexpr) |
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) | (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) | (C++11부터) (C++20부터 constexpr) |
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) | (C++11부터) (C++20부터 constexpr) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( CharT lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(15) | (C++11부터) (C++20부터 constexpr) |
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) | (C++26부터) |
lhs의 문자 뒤에 rhs의 문자가 이어지는 문자열을 반환합니다. 다음과 동일합니다:
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); 단, lhs와 rhs 모두 유효하지만 지정되지 않은 상태로 남습니다. lhs와 rhs의 할당자가 같으면, 구현이 둘 중 하나에서 이동할 수 있습니다.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);|
결과에 사용되는 할당자는 다음과 같습니다: 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()다시 말해:
각 경우, 둘 다 같은 값 종류의 의 경우(8-16), 모든 rvalue |
(C++11부터) |
매개변수
| 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 |
반환값
lhs 의 문자들을 포함하고 그 뒤에 rhs 의 문자들이 이어지는 문자열 , 위에서 결정된 할당자를 사용하여 (C++11부터) .
참고 사항
using my_string = std::basic_string<char, std::char_traits<char>, my_allocator<char>>; my_string cat(); const my_string& dog(); my_string meow = /* ... */, woof = /* ... */; meow + cat() + /* ... */; // meow의 allocator에 select_on_container_copy_construction 사용 woof + dog() + /* ... */; // 대신 dog() 반환 값의 allocator 사용 meow + woof + meow; // meow의 allocator에 select_on_container_copy_construction 사용 meow + (woof + meow); // 대신 woof의 allocator에 SOCCC 사용
// 최종 결과에 my_favorite_allocator 사용 my_string(my_favorite_allocator) + meow + woof + cat() + dog();
allocator를 더 좋고 이식성 있게 제어하려면, 원하는 allocator로 생성된 결과 문자열에서
|
(C++11부터) |
|
std::
type_identity_t
의 사용을 오버로드
(
4
)
,
(
7
)
,
(
12
)
및
(
16
)
에서 매개변수로 사용함으로써,
std::
basic_string
<
CharT, Traits, Allocator
>
타입의 객체가 항상
|
(C++26부터) |
예제
#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!
결함 보고서
다음 동작 변경 결함 보고서는 이전에 게시된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| P1165R1 | C++11 | 할당자 전파가 불규칙하고 일관성이 없음 | 더 일관성 있게 개선됨 |
참고 항목
|
끝에 문자를 추가함
(public member function) |
|
|
끝에 문자를 추가함
(public member function) |
|
|
문자를 삽입함
(public member function) |