This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [PATCH] Assign(__str, __pos, __n) (take3) + Assign(__s, __n)
- From: Nathan Myers <ncm-nospam at cantrip dot org>
- To: libstdc++ at gcc dot gnu dot org
- Date: Sat, 15 Dec 2001 19:40:57 +0000
- Subject: Re: [PATCH] Assign(__str, __pos, __n) (take3) + Assign(__s, __n)
- References: <3C1B6AC9.BE4BCEF9@unitus.it>
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