This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: basic_string<> - operator+


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]