 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Heinz Ozwirk Guest
|
Posted: Sat Oct 29, 2005 7:14 am Post subject: Type and value of basic_string::npos |
|
|
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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sat Oct 29, 2005 7:19 am Post subject: Re: Type and value of basic_string::npos |
|
|
Heinz Ozwirk wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Jonathan Mcdougall Guest
|
Posted: Sat Oct 29, 2005 7:22 am Post subject: Re: Type and value of basic_string::npos |
|
|
Heinz Ozwirk wrote:
| Quote: | Does the standard require that
1. the type of basic_string::npos is an unsigned type?
|
Yes.
| Quote: | 2. the value of basic_string::npos is the largest possible value of that
type?
|
Yes. It is actually required to be -1.
| Quote: | 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
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Sat Oct 29, 2005 2:15 pm Post subject: Re: Type and value of basic_string::npos |
|
|
"Heinz Ozwirk" <hozwirk.SPAM (AT) arcor (DOT) de> 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.
: 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
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Sat Oct 29, 2005 2:26 pm Post subject: Re: Type and value of basic_string::npos |
|
|
Ivan Vecerina wrote:
| Quote: | 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)
|
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Sat Oct 29, 2005 3:34 pm Post subject: Re: Type and value of basic_string::npos |
|
|
Pete Becker wrote:
| Quote: | 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)<
outputs 65536 on vc7.1, g++ 3.4 and intel 9, as I expected.
--
Valentin Samko - http://www.valentinsamko.com
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Sat Oct 29, 2005 5:14 pm Post subject: Re: Type and value of basic_string::npos |
|
|
Valentin Samko wrote:
| Quote: | 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)
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Sun Oct 30, 2005 4:50 am Post subject: Re: Type and value of basic_string::npos |
|
|
"Valentin Samko" <c++.moderated (AT) digiways (DOT) com> 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.
:
: typedef unsigned short ushort;
: std::cout<<(ushort(-1) + 1)<
:
: 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|