Type and value of basic_string::npos

Discussion in 'C++' started by Heinz Ozwirk, Oct 29, 2005.

  1. Heinz Ozwirk

    Heinz Ozwirk Guest

    Does the standard require that

    1. the type of basic_string::npos is an unsigned type?
    2. the value of basic_string::npos is the largest possible value of that
    type?

    And - only if both are true - basic_string::npos + 1 == 0 ?

    Tnx
    Heinz
     
    Heinz Ozwirk, Oct 29, 2005
    #1
    1. Advertising

  2. Heinz Ozwirk wrote:
    > Does the standard require that
    >
    > 1. the type of basic_string::npos is an unsigned type?
    > 2. the value of basic_string::npos is the largest possible value of that
    > type?
    >
    > And - only if both are true - basic_string::npos + 1 == 0 ?
    >
    > Tnx
    > Heinz
    >
    >


    Yes, yes and yes.

    john
     
    John Harrison, Oct 29, 2005
    #2
    1. Advertising

  3. Heinz Ozwirk wrote:
    > Does the standard require that
    >
    > 1. the type of basic_string::npos is an unsigned type?


    Yes.

    > 2. the value of basic_string::npos is the largest possible value of that
    > type?


    Yes. It is actually required to be -1.

    > And - only if both are true - basic_string::npos + 1 == 0 ?


    Yes. However, npos is required to be an unsigned *integral*, no more.
    Be careful to use only values of type basic_string::size_type when you
    compare them to npos. A comparison with an unsigned int (or whatever
    other type) is not portable.


    Jonathan
     
    Jonathan Mcdougall, Oct 29, 2005
    #3
  4. "Heinz Ozwirk" <> wrote in message
    news:4363216a$0$21956$-online.net...
    : Does the standard require that
    :
    : 1. the type of basic_string::npos is an unsigned type?
    Yes.
    : 2. the value of basic_string::npos is the largest possible value of
    that
    : type?
    Yes.

    : And - only if both are true - basic_string::npos + 1 == 0 ?
    Not necessarily, as far as I understand.
    If basic_string::npos is of type 'unsigned short' (16 bits),
    and int is 32 bits, then npos+1 would be promoted to
    a (32-bit) int, and the result would be: 65536

    The following however shall always be true:
    ~basic_string::npos == 0


    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact
    form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Oct 29, 2005
    #4
  5. Heinz Ozwirk

    Pete Becker Guest

    Ivan Vecerina wrote:
    > If basic_string::npos is of type 'unsigned short' (16 bits),
    > and int is 32 bits, then npos+1 would be promoted to
    > a (32-bit) int, and the result would be: 65536
    >


    That's the right answer, but you've got the promotion in the wrong
    place. Since 1 is of type int and npos is of type unsigned short (in
    this example), npos will be promoted to int. Once that promotion has
    been done, the sum has type int.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Oct 29, 2005
    #5
  6. Pete Becker wrote:
    > Ivan Vecerina wrote:
    >> If basic_string::npos is of type 'unsigned short' (16 bits),
    >> and int is 32 bits, then npos+1 would be promoted to
    >> a (32-bit) int, and the result would be: 65536
    >>

    >
    > That's the right answer, but you've got the promotion in the wrong
    > place. Since 1 is of type int and npos is of type unsigned short (in
    > this example), npos will be promoted to int. Once that promotion has
    > been done, the sum has type int.


    But still, when unsigned 16bit integer which was assigned -1 is promoted to int, we get
    65535 in that int. And then we add 1 to that, getting 65536.

    typedef unsigned short ushort;
    std::cout<<(ushort(-1) + 1)<<std::endl;

    outputs 65536 on vc7.1, g++ 3.4 and intel 9, as I expected.


    --

    Valentin Samko - http://www.valentinsamko.com
     
    Valentin Samko, Oct 29, 2005
    #6
  7. Heinz Ozwirk

    Pete Becker Guest

    Valentin Samko wrote:

    > Pete Becker wrote:
    >
    >> Ivan Vecerina wrote:
    >>
    >>> If basic_string::npos is of type 'unsigned short' (16 bits),
    >>> and int is 32 bits, then npos+1 would be promoted to
    >>> a (32-bit) int, and the result would be: 65536
    >>>

    >>
    >> That's the right answer, but you've got the promotion in the wrong
    >> place. Since 1 is of type int and npos is of type unsigned short (in
    >> this example), npos will be promoted to int. Once that promotion has
    >> been done, the sum has type int.

    >
    >
    > But still, when unsigned 16bit integer which was assigned -1 is promoted
    > to int, we get 65535 in that int. And then we add 1 to that, getting 65536.
    >


    Yes, that's what "That's the right answer" means.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Oct 29, 2005
    #7
  8. "Valentin Samko" <> wrote in message
    news:43639575$0$5396$...
    : Pete Becker wrote:
    : > Ivan Vecerina wrote:
    : >> If basic_string::npos is of type 'unsigned short' (16 bits),
    : >> and int is 32 bits, then npos+1 would be promoted to
    : >> a (32-bit) int, and the result would be: 65536
    : >>
    : >
    : > That's the right answer, but you've got the promotion in the wrong
    : > place. Since 1 is of type int and npos is of type unsigned short
    (in
    : > this example), npos will be promoted to int. Once that promotion
    has
    : > been done, the sum has type int.
    :
    : But still, when unsigned 16bit integer which was assigned -1
    : is promoted to int, we get
    : 65535 in that int. And then we add 1 to that, getting 65536.
    :
    : typedef unsigned short ushort;
    : std::cout<<(ushort(-1) + 1)<<std::endl;
    :
    : outputs 65536 on vc7.1, g++ 3.4 and intel 9, as I expected.

    Yes.

    What Pete's point was is that the following statement I made
    was inaccurate: << npos+1 would be promoted to a (32-bit) int >>
    It is 'npos' itself that is first promoted to an int *prior* to
    adding the integer value '1'.

    Lack of clarity on my side -- pointed out by Pete in his personal
    style
    that often makes it sound like everything previously said is 'crap'.

    Cheers,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact
    form
     
    Ivan Vecerina, Oct 30, 2005
    #8
    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,742
    Jorge Rivera
    Apr 15, 2004
  2. Marcus Kwok
    Replies:
    7
    Views:
    448
    Duane Hebert
    Jan 27, 2006
  3. John Fly
    Replies:
    1
    Views:
    446
    Archmagus
    Feb 17, 2006
  4. Alien
    Replies:
    5
    Views:
    577
    Pete Becker
    Sep 18, 2006
  5. Marcus Kwok
    Replies:
    9
    Views:
    430
    Marcus Kwok
    Jul 2, 2007
Loading...

Share This Page