This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Re: basic_string<> - operator+
- To: Benjamin Kosnik <bkoz@cygnus.com>
- Subject: Re: basic_string<> - operator+
- From: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
- Date: Fri, 25 Jun 1999 14:00:08 +0200 (CEST)
- cc: libstdc++@sourceware.cygnus.com
- Reply-To: Ryszard Kabatek <kabatek@chemie.uni-halle.de>
On Fri, 25 Jun 1999, Benjamin Kosnik wrote:
> This may be a win though, in the case of the single _CharT:
>
> template<typename _CharT, typename _Traits, typename _Alloc>
> inline basic_string<_CharT, _Traits, _Alloc>
> operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>&
> __rhs)
> {
> basic_string<_CharT, _Traits, _Alloc> __str(__rhs);
> __str.insert(__str.begin(), __lhs);
> return __str;
> }
This is a WRONG way!
You call the non-const begin function - it makes __str unsharable.
Please change it to:
typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
__str.insert(static_cast<size_type>(0), static_cast<size_type>(1), __lhs);
Ryszard Kabatek
Martin-Luther University Halle-Wittenberg, Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 (2466) Fax. +49 3461 46 2129