function template
<string>

std::operator+ (basic_string)

string (1)
template <class charT, class traits, class Alloc>
  basic_string operator+ (const basic_string<charT,traits,Alloc>& lhs,
                          const basic_string<charT,traits,Alloc>& rhs);
c-string (2)
template <class charT, class traits, class Alloc>
  basic_string operator+ (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);
character (3)
template <class charT, class traits, class Alloc>
  basic_string operator+ (const basic_string<charT,traits,Alloc>& lhs, charT rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (charT lhs, const basic_string<charT,traits,Alloc>& rhs);
string (1)
template <class charT, class traits, class Alloc>
  basic_string operator+ (const basic_string<charT,traits,Alloc>& lhs,
                          const basic_string<charT,traits,Alloc>& rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (basic_string<charT,traits,Alloc>&& lhs,
                          basic_string<charT,traits,Alloc>&& rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (basic_string<charT,traits,Alloc>&& lhs,
                          const basic_string<charT,traits,Alloc>& rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (const basic_string<charT,traits,Alloc>& lhs,
                          basic_string<charT,traits,Alloc>&& rhs);
c-string (2)
template <class charT, class traits, class Alloc>
  basic_string operator+ (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (basic_string<charT,traits,Alloc>&& lhs, const charT* rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (const charT* lhs, const basic_string<charT,traits,Alloc>&  rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (const charT* lhs, basic_string<charT,traits,Alloc>&& rhs);
character (3)
template <class charT, class traits, class Alloc>
  basic_string operator+ (const basic_string<charT,traits,Alloc>& lhs, charT rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (basic_string<charT,traits,Alloc>&& lhs, charT rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (charT lhs, const basic_string<charT,traits,Alloc>& rhs);
template <class charT, class traits, class Alloc>
  basic_string operator+ (charT lhs, basic_string<charT,traits,Alloc>&& rhs);
Concatenate strings
Returns a newly constructed basic_string object with its value being the concatenation of the characters in lhs followed by those of rhs.

In the signatures taking at least one rvalue reference as argument, the returned object is move-constructed by passing this argument, which is left in an unspecified but valid state. If both arguments are rvalue references, only one of them is moved (it is unspecified which), with the other one preserving its value.

Parameters

lhs, rhs
Arguments to the left- and right-hand side of the operator, respectively.
If of type charT*, it shall point to a null-terminated character sequence.
charT is basic_string's character type (i.e., its first template parameter).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// concatenating strings
#include <iostream>
#include <string>

main ()
{
  std::string firstlevel ("com");
  std::string secondlevel ("cplusplus");
  std::string scheme ("http://");
  std::string hostname;
  std::string url;

  hostname = "www." + secondlevel + '.' + firstlevel;
  url = scheme + hostname;

  std::cout << url << '\n';

  return 0;
}


Output:
http://www.cplusplus.com

Return Value

A basic_string object whose value is the concatenation of lhs and rhs.

Complexity

Unspecified, but generally linear in the resulting string length (and linear in the length of the non-moved argument for signatures with rvalue references).

Iterator validity

The signatures with rvalue references may invalidate iterators, pointers and references related to the moved basic_string object.

Data races

The signatures with rvalue references modify the moved basic_string object.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in either basic_string objects.

If s is not a null-terminated character sequence, it causes undefined behavior.

If the resulting string length would exceed the max_size, a length_error exception is thrown.
If the type uses the default allocator, a bad_alloc exception is thrown if the function needs to allocate storage and fails.

See also