[PATCH] Assign(__str, __pos, __n) (take3) + Assign(__s, __n)

Nathan Myers ncm-nospam@cantrip.org
Sat Dec 15 11:40:00 GMT 2001


On Sat, Dec 15, 2001 at 04:22:49PM +0100, Paolo Carlini wrote:
> ... In the meanwhile I have also prepared the new
> assign(const _CharT* __s, size_type __n) > (similar general structure),
> based on http://gcc.gnu.org/ml/libstdc++/2001-12/msg00215.html.
> 
> !  if (__n > this->max_size())
> !    __throw_length_error("basic_string::assign");
> !  if (_M_rep()->_M_is_shared() || __s < _M_data()
> !      || __s > _M_data() + this->size())
> !    return _M_replace_safe(_M_ibegin(), _M_iend(), __s, __s + __n);
> !  else 

Two thoughts.  First, for portability we cannot compare two pointers
directly that are not pointing into the same array.  (The mistake may 
occur in the existing code, mea culpa.)  However, we can use std::less<>
to do the same thing safely.  Second, this version is more general and
might be used to implement the other, although at the cost of an extra
comparison.

Nathan Myers
ncm at cantrip dot org



More information about the Libstdc++ mailing list