Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it correct to call string::substr(npos+1)?

Reply
Thread Tools

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

 
 
Alien
Guest
Posts: n/a
 
      09-18-2006
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.

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      09-18-2006
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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-18-2006
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
 
Reply With Quote
 
Alien
Guest
Posts: n/a
 
      09-18-2006
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


 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      09-18-2006

> 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

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-18-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
Uploaded File Empty but in correct folder with correct name froil Perl Misc 12 03-02-2006 01:21 PM
Correct White Balance Doesn't Mean Correct Color?? jim evans Digital Photography 28 12-27-2005 05:10 AM
correct or not correct? Dan HTML 7 10-02-2003 10:16 PM
To correct my program. please, check to find errors and correct me. joon Java 1 07-08-2003 06:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57