Re: basic_string::npos

From: Andrey Tarasevich (andreytarasevich_at_hotmail.com)
Date: 10/30/03


Date: Thu, 30 Oct 2003 14:34:52 -0800

Gene Wirchenko wrote:
> ...
> basic_string::npos
> static const size_type npos = -1;
> The constant is the largest representable value of type size_type. It
> is assuredly larger than max_size(), hence it serves as either a very
> large value or as a special code."
>
> 1) -1 is assuredly larger than the length of the longest sequence
> that a string can control?

No, '-1' isn't. But the value of 'npos' after initializing it with '-1' is.

The important detail here is that 'std::basic_string<>::size_type' is an
_unsigned_ type. Unsigned types in C++ implement arithmetic modulo 2^n,
where 'n' is the number of bits in the value representation of the type.
For this reason, initializing a value of unsigned type with '-1' is the
same as initializing it with maximum value of that unsigned type.

> 2) Despite
> #include <string>
> I am not able to refer to this value anyway.

How are you trying to refer to it? Post a short example.

-- 
Best regards,
Andrey Tarasevich


Relevant Pages

  • Re: a DWORD with all bits set...
    ... The representation of -1 does not matter for the purpose of the ... does *not* under one's complement, sign/magnitude, and other ... on conversion to an unsigned type. ... expanding it to a 32 bit DWORD only handles half of it. ...
    (microsoft.public.vc.mfc)
  • Re: a DWORD with all bits set...
    ... The representation of -1 does not matter for the purpose of the ... does *not* under one's complement, sign/magnitude, and other ... on conversion to an unsigned type. ... expanding it to a 32 bit DWORD only handles half of it. ...
    (microsoft.public.vc.mfc)
  • Re: ((m) /((m)%255+1)/255%255*8 + 7-100/((m)%255+14))
    ... pure binary representation; this shall be known as the value ... If some combination of value bits was a trap representation then ... Those bits have the same value as in the unsigned type, ... Because that clause is discussing only unsigned types. ...
    (comp.lang.c)
  • Re: ((m) /((m)%255+1)/255%255*8 + 7-100/((m)%255+14))
    ... pure binary representation; this shall be known as the value ... Those bits have the same value as in the unsigned type, ... not as strict in it's requirements for the ranges of signed integer ... of trap representations were introduced. ...
    (comp.lang.c)
  • Re: SIZE_MAX under c89
    ... > In case of maybe C99 compiler: ... > #ifndef SIZE_MAX ... > to unsigned type values? ... padding bits and other representation ...
    (comp.lang.c)