473,804 Members | 3,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type and value of basic_string::n pos

Does the standard require that

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

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

Tnx
Heinz
Oct 29 '05 #1
7 2632
Heinz Ozwirk wrote:
Does the standard require that

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

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

Tnx
Heinz


Yes, yes and yes.

john
Oct 29 '05 #2
Heinz Ozwirk wrote:
Does the standard require that

1. the type of basic_string::n pos is an unsigned type?
Yes.
2. the value of basic_string::n pos is the largest possible value of that
type?
Yes. It is actually required to be -1.
And - only if both are true - basic_string::n pos + 1 == 0 ?


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

Oct 29 '05 #3
"Heinz Ozwirk" <ho**********@a rcor.de> wrote in message
news:43******** *************** @newsread2.arco r-online.net...
: Does the standard require that
:
: 1. the type of basic_string::n pos is an unsigned type?
Yes.
: 2. the value of basic_string::n pos is the largest possible value of
that
: type?
Yes.

: And - only if both are true - basic_string::n pos + 1 == 0 ?
Not necessarily, as far as I understand.
If basic_string::n pos 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
Oct 29 '05 #4
Ivan Vecerina wrote:
If basic_string::n pos 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)
Oct 29 '05 #5
Pete Becker wrote:
Ivan Vecerina wrote:
If basic_string::n pos 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<<(ush ort(-1) + 1)<<std::endl;

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

Valentin Samko - http://www.valentinsamko.com
Oct 29 '05 #6
Valentin Samko wrote:
Pete Becker wrote:
Ivan Vecerina wrote:
If basic_string::n pos 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)
Oct 29 '05 #7
"Valentin Samko" <c+***********@ digiways.com> wrote in message
news:43******** **************@ authen.white.re adfreenews.net. ..
: Pete Becker wrote:
: > Ivan Vecerina wrote:
: >> If basic_string::n pos 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<<(ush ort(-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
Oct 30 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6247
by: Dave | last post by:
Hello all, Suppose you have an unsigned integral type T. It's not one of the built-in types, but rather typedefed off of one of the built-in unsigned integral types (but we don't know which one). I want to find the maximum value for this type. This seems to work just fine: static_cast<T>(-1)
8
518
by: Derek | last post by:
A common technique for trimming leading and trailing spaces from std::string is the following: string s(" blah blah blah "); const char* ws= " \t\r"; string::size_type not_white; // trim leading whitespace not_white = s.find_first_not_of(ws);
0
1414
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
1
1345
by: Phil Endecott | last post by:
Dear C++ Experts, What type should I use for an index into a std::string? In the past I have used an unsigned int, e.g. unsigned int p = s.find("foo"); string t = s.substr(p,10); etc. etc. but I now find that this doesn't work on 64-bit machines; I get warnings
7
4871
by: Marcus Kwok | last post by:
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the "all characters" marker. I tried to use it this way, but my program crashes: #include <iostream> #include <string> int main() { std::string s = "hi/hello/now";
5
3012
by: Alien | last post by:
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.
8
2543
by: Marcus Kwok | last post by:
Is std::string::npos portably able to be incremented? For example, I want to insert some text into a string. If a certain character is found, I want to insert this text immediately after this character; otherwise I insert at the beginning of the string. On my implementation string::npos has the value of (string::size_type)-1, so incrementing it will make it 0, but can I rely on it? Also, say that the character occurs at the end of...
7
2439
by: Adrian | last post by:
Why does std::strings find search from the begining of the string when pos >= (std::string::npos-3) I cant find anything in the standard that says what find should do if pos==npos in find I tried it on a few platforms (all with gcc unfortunaley) and its seems to be consistent. Adrian
7
4272
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant expression") or Comeau Online ("constant value is not known"). If I replace
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.