<html>
<head>
<title>basic_string operator &lt;&lt;</title>
</head>

<body>
<h1>basic_string operator &lt;&lt;</h1>

<p>ISO/IEC JTC1 SC22 WG21 N2233 = 07-0093 - 2007-04-27 

<p>Lawrence Crowl

<h2>Problem</h2>

<p>There are two problems with the current string concatenation syntax.

<dl>
<dt>awkward</dt>
<dd>String concatenation has an awkward standard syntax,
requiring parentheses in proportion to the number of items concatenated.
For example,
<blockquote><pre><code>
std::string x, y;
((x += "a") += 'b') += y;
x.append("a").append('b').append(y);
</code></pre></blockquote>
</dd>

<dt>distinct</dt>
<dd>There is a potential concept for "simple stream-like" objects,
which have the notion of appending strings.
The existing <code>basic_string</code> has a syntax
that is distinct from similar operations on streams,
thus requiring, at minimum, a concept map.
</dd>
</dl>

<h2>Solution</h2>

<p>We propose to add
<code>operator&nbsp;&lt;&lt;</code> member functions
that are otherwise identical to the existing 
<code>operator&nbsp;+=</code> member functions.

<p>The resulting concatenation syntax requires no parentheses.
<blockquote><pre><code>
std::string x, y;
x << "a" << 'b' << y;
</code></pre></blockquote>

<p>The resulting <code>&lt;&lt;</code> operator syntax
is common with stream.

<h2>Non-Solutions</h2>

<p>We specifically do not propose to add formatting operations to strings.
For this purpose, stringstream is the right solution.

<h2>Changes to the C++ Standard</h2>

<h3>21.3 Class template basic_string [basic.string]</h3>

<p>To paragraph 5, subparagraph "21.3.6 modifiers",
after the <code>+=</code> operators, add

<blockquote><pre><code>
basic_string&amp; operator&lt;&lt;(const basic_string&amp; <var>str</var>);
basic_string&amp; operator&lt;&lt;(const charT* <var>s</var>);
basic_string&amp; operator&lt;&lt;(charT <var>c</var>);
</blockquote>

<h3>21.3.6.new basic_string::operator&lt;&lt; [string::op&lt;&lt;]</h3>

<p>After section 21.3.6.1 basic_string::operator+= [string::op+=],
add the following section.

<p><code>basic_string<charT,traits,Allocator>&amp;
operator+=(const basic_string<charT,traits,Allocator>&amp;
<var>str</var>);</code>

<p><em>Returns:</em>
<code>append(<var>str</var>)<code>.

<p><code>basic_string<charT,traits,Allocator>&amp;
operator+=(const charT* <var>s</var>);</code>

<p></em>Returns:</em>
<code>append(<var>s</var>).

<p><code>basic_string<charT,traits,Allocator>&amp;
operator+=(charT <var>c</var>);

<p><em>Returns:</em>
<code>append(1,<var>c</var>).</code>

</body>
</html>
