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[] const


> Is it neccesary to check for __pos != size() ?
> Why only for the const version?
> Is this requierd by the standard?
> 
> BTW. If we check for !=, so we can check for < instead.
> 
>       // Element access:
>       const_reference 
>       operator[] (size_type __pos) const 
>       {
>         if (__pos != this->size())    // Why not: if (__pos < this->size())
>           return _M_data()[__pos]; 
>         else
>           return _Rep::_S_terminal;
>       }

Indeed, why check at all?  We are careful to plug in the
right value at that place when constructing the string, 
and don't (or at least didn't) allow it to be overwritten.

Irritatingly, the original spec left the value at that location
undefined, allowing an implementation to report an error if it was 
looked at, and then when I wasn't looking the library subgroup
changed it.   Grrrr.

Nathan Myers
ncm@cantrip.org


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