Is it correct to call string::substr(npos+1)?

Discussion in 'C++' started by Alien, Sep 18, 2006.

  1. Alien

    Alien Guest

    npos is defined as -1, in both SGI and VS.
    So npos+1=0?
    I remember that I read a book saying that npos+1==npos.
    Alien, Sep 18, 2006
    #1
    1. Advertising

  2. Alien wrote:


    > npos is defined as -1, in both SGI and VS.
    > So npos+1=0?
    > I remember that I read a book saying that npos+1==npos.


    The value of npos is implementation defined. On many platforms it is
    -1, but there is no requirement that that be the case. So don't rely
    on it.

    Best regards,

    Tom
    Thomas Tutone, Sep 18, 2006
    #2
    1. Advertising

  3. Alien

    Kai-Uwe Bux Guest

    Alien wrote:

    > npos is defined as -1, in both SGI and VS.


    Actually, it is defined that way in the standard [21.3/6]


    > So npos+1=0?


    As long as npos is an unsigned integral type (which it is): yes.


    > I remember that I read a book saying that npos+1==npos.


    Well, that would be false.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 18, 2006
    #3
  4. Alien

    Alien Guest

    So, it's not defined in the standard?

    Thomas Tutone wrote:
    > Alien wrote:
    >
    >
    > > npos is defined as -1, in both SGI and VS.
    > > So npos+1=0?
    > > I remember that I read a book saying that npos+1==npos.

    >
    > The value of npos is implementation defined. On many platforms it is
    > -1, but there is no requirement that that be the case. So don't rely
    > on it.
    >
    > Best regards,
    >
    > Tom
    Alien, Sep 18, 2006
    #4

  5. > Thomas Tutone wrote:
    > > Alien wrote:
    > >
    > >
    > > > npos is defined as -1, in both SGI and VS.
    > > > So npos+1=0?
    > > > I remember that I read a book saying that npos+1==npos.

    > >
    > > The value of npos is implementation defined. On many platforms it is
    > > -1, but there is no requirement that that be the case. So don't rely
    > > on it.


    Alien wrote:
    > So, it's not defined in the standard?


    Kai-Uwe Bux has proven me wrong. It's defined in std::basic_string as
    const size_type npos = -1;

    I would have sworn Jossutis said it was implementation-defined.

    Sorry to lead you astray.

    Best regards,

    Tom
    Thomas Tutone, Sep 18, 2006
    #5
  6. Alien

    Pete Becker Guest

    Thomas Tutone wrote:
    >> Thomas Tutone wrote:
    >>> Alien wrote:
    >>>
    >>>
    >>>> npos is defined as -1, in both SGI and VS.
    >>>> So npos+1=0?
    >>>> I remember that I read a book saying that npos+1==npos.
    >>> The value of npos is implementation defined. On many platforms it is
    >>> -1, but there is no requirement that that be the case. So don't rely
    >>> on it.

    >
    > Alien wrote:
    >> So, it's not defined in the standard?

    >
    > Kai-Uwe Bux has proven me wrong. It's defined in std::basic_string as
    > const size_type npos = -1;
    >
    > I would have sworn Jossutis said it was implementation-defined.
    >


    He should have been right, except that it was originaly unspecified, not
    implementation defined. Its purpose was to provide a sentinel value,
    nothing more. Somehow unspecified got changed to -1, and that's led far
    too many people to write code based on that particular value.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Sep 18, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek

    string::npos + 1 ???

    Derek, Apr 12, 2004, in forum: C++
    Replies:
    8
    Views:
    4,690
    Jorge Rivera
    Apr 15, 2004
  2. Marcus Kwok
    Replies:
    7
    Views:
    421
    Duane Hebert
    Jan 27, 2006
  3. John Fly
    Replies:
    1
    Views:
    439
    Archmagus
    Feb 17, 2006
  4. Adrian
    Replies:
    7
    Views:
    434
    Adrian
    Jul 12, 2007
  5. Christopher Pisz
    Replies:
    11
    Views:
    535
    James Kanze
    Jan 13, 2008
Loading...

Share This Page