Namespaces
Variants

std::operator+(std::basic_string)

cppreference.net에서 가져옴
 
 
 
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의 문자가 이어지는 문자열을 반환합니다. 다음과 동일합니다:

1,2) std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;
3) std::basic_string<CharT, Traits, Allocator> r = lhs; r.push_back(rhs); return r;
4) std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;
5) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;
6) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(r.begin(), lhs); return r;
7) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;
8) lhs.append(rhs); return std::move(lhs); 단, lhs와 rhs 모두 유효하지만 지정되지 않은 상태로 남습니다. lhs와 rhs의 할당자가 같으면, 구현이 둘 중 하나에서 이동할 수 있습니다.
9,10) lhs.append(rhs); return std::move(lhs);
11) lhs.push_back(rhs); return std::move(lhs);
12) lhs.append(rhs); return std::move(lhs);
13,14) rhs.insert(0, lhs); return std::move(rhs);
15) rhs.insert(rhs.begin(), lhs); return std::move(rhs);
16) 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()

다시 말해:

  • 한쪽 피연산자가 basic_string rvalue이면, 해당 할당자가 사용됩니다.
  • 그렇지 않으면, lvalue select_on_container_copy_construction 피연산자의 할당자에 basic_string가 사용됩니다.

각 경우, 둘 다 같은 값 종류의 basic_string일 때 왼쪽 피연산자가 선호됩니다.

의 경우(8-16), 모든 rvalue basic_string 피연산자는 유효하지만 지정되지 않은 상태로 남습니다.

(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부터) .

참고 사항

operator+ 는 상태 저장 allocator가 관련된 경우 매우 주의해서 사용해야 합니다 (예: std::pmr::string 사용 시) (C++17부터) . P1165R1 이전에는 결과에 사용되는 allocator가 역사적 우연에 의해 결정되었으며 명백한 이유 없이 오버로드마다 달라질 수 있었습니다. 또한 ( 1-5 ) 의 경우 allocator 전파 동작이 주요 표준 라이브러리 구현체마다 다르며 표준에 명시된 동작과도 다릅니다.

operator+ 의 결과에 사용되는 allocator가 값 범주(value category)에 민감하기 때문에, operator+ 는 allocator 전파 측면에서 결합 법칙이 성립하지 않습니다:

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 사용

operator+ 호출 체인에 대해 최종 결과에 사용되는 allocator는 원하는 allocator를 가진 rvalue basic_string 을 앞에 추가하여 제어할 수 있습니다:

// 최종 결과에 my_favorite_allocator 사용
my_string(my_favorite_allocator) + meow + woof + cat() + dog();

allocator를 더 좋고 이식성 있게 제어하려면, 원하는 allocator로 생성된 결과 문자열에서 append , insert , operator+= 와 같은 멤버 함수를 사용해야 합니다.

(C++11부터)

std:: type_identity_t 의 사용을 오버로드 ( 4 ) , ( 7 ) , ( 12 ) 및 ( 16 ) 에서 매개변수로 사용함으로써, std:: basic_string < CharT, Traits, Allocator > 타입의 객체가 항상 T 타입의 객체와 연결될 수 있도록 보장하며, 이때 T 는 std:: basic_string_view < CharT, Traits > 로의 암시적 변환이 가능합니다. 역의 경우도 마찬가지이며, 이는 오버로드 해결 규칙에 따릅니다.

기능 테스트 매크로 값 표준 기능
__cpp_lib_string_view 202403 (C++26) 문자열과 문자열 뷰의 연결, 오버로드 ( 4 ) , ( 7 ) , ( 12 ) , ( 16 )
(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)