Topic: Q: is string::npos defined to be -1?
Author: Rob Stewart <donotspamme@giage.com>
Date: 2000/04/15 Raw View
Robert Klemme wrote:
>
> Fernando Luis Cacciola Carballal schrieb:
> >
> > I am participating in a thread about trimming whitespaces in a string on
> > comp.lang.c++.moderated.
> > I claimed that the call s.erase ( s.find_last_not_of ( t ) + 1 ) is always
> > safe since basic_string::npos is defined to be 'the largest value of type
> > size_t', which implies that npos+1 is guaranteed to be 0.
> >
> > Am I correct? Or a portable implementation should test for inequality to
> > npos before calling erase()?
>
> independant whether this is so or not i would not use such idioms. i
> would not calculate with this special constant and just use it for
> comparisons since this is used for reporting or announcement of special
> conditions.
But there is no calculation with that special value per se. The
result of find_last_not_of() et. al. is incremented to create the
correct index for erase(). If find_last_not_of() returns npos,
then the effect is to erase the whole string, precisely as
desired.
The comparision is done by erase to decide what to erase. The
algorithm presented doesn't need a comparison. To do otherwise
would increase the size of the function unecessarily.
If relying on this behavior is troubling, a simple design note
comment can explain it for posterity.
--
Robert Stewart | rob-at-giage-dot-com
Software Engineer | using std::disclaimer;
Giage, Ltd. | http://www.giage.com
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: John Aldridge <jpsa@jjdash.demon.co.uk>
Date: 2000/04/12 Raw View
In article <8cva8u$12o3@mail05.knox.edu>, Fernando Luis Cacciola
Carballal <fcacciola@fibertel.com.ar> writes
>
>I claimed that the call s.erase ( s.find_last_not_of ( t ) + 1 ) is always
>safe since basic_string::npos is defined to be 'the largest value of type
>size_t', which implies that npos+1 is guaranteed to be 0.
>
>However, reader Rob Stewart looked it up and didn't find a precise
>definition for npos that guarantees the above.
Section 21.3, para 6
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_string {
:
static const size_type npos = -1;
:
--
Cheers,
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/04/13 Raw View
John Aldridge wrote:
>
> In article <8cva8u$12o3@mail05.knox.edu>, Fernando Luis Cacciola
> Carballal <fcacciola@fibertel.com.ar> writes
> >
> >I claimed that the call s.erase ( s.find_last_not_of ( t ) + 1 ) is always
> >safe since basic_string::npos is defined to be 'the largest value of type
> >size_t', which implies that npos+1 is guaranteed to be 0.
> >
> >However, reader Rob Stewart looked it up and didn't find a precise
> >definition for npos that guarantees the above.
>
> Section 21.3, para 6
>
> namespace std {
> template<class charT, class traits = char_traits<charT>,
> class Allocator = allocator<charT> >
> class basic_string {
> :
> static const size_type npos = -1;
> :
Note that since size_type is unsigned, this means that npos is indeed
"the largest value of type size_t".
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/04/13 Raw View
James Kuyper wrote:
>
> John Aldridge wrote:
> >
> > In article <8cva8u$12o3@mail05.knox.edu>, Fernando Luis Cacciola
> > Carballal <fcacciola@fibertel.com.ar> writes
> > >
> > >I claimed that the call s.erase ( s.find_last_not_of ( t ) + 1 ) is always
> > >safe since basic_string::npos is defined to be 'the largest value of type
> > >size_t', which implies that npos+1 is guaranteed to be 0.
> > >
> > >However, reader Rob Stewart looked it up and didn't find a precise
> > >definition for npos that guarantees the above.
> >
> > Section 21.3, para 6
> >
> > namespace std {
> > template<class charT, class traits = char_traits<charT>,
> > class Allocator = allocator<charT> >
> > class basic_string {
> > :
> > static const size_type npos = -1;
> > :
>
> Note that since size_type is unsigned, this means that npos is indeed
> "the largest value of type size_t".
My apologies: the quoted text was cut-and-pasted, and I missed a typo. I
meant to say "the largest value of type size_type".
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Rob Stewart <donotspamme@giage.com>
Date: 2000/04/13 Raw View
Matt Austern wrote:
>
> "Fernando Luis Cacciola Carballal" <fcacciola@fibertel.com.ar> writes:
>
> > I am participating in a thread about trimming whitespaces in a string on
> > comp.lang.c++.moderated.
> > I claimed that the call s.erase ( s.find_last_not_of ( t ) + 1 ) is always
> > safe since basic_string::npos is defined to be 'the largest value of type
> > size_t', which implies that npos+1 is guaranteed to be 0.
>
> The basic_string<> synopsis, at the end of section 21.3, says that
> npos is defined as
> static const size_type npos = -1;.
That's what I was looking at, but it wasn't clear to me that the
standard thus required npos to be -1.
Since standard containers must use size_t for size_type, and npos
must be -1, and size_t is unsigned, and we know that incrementing
the largest unsigned value must "wrap around" to zero, then
Fernando's technique is sound.
Thanks for your clarification.
--
Robert Stewart | rob-at-giage-dot-com
Software Engineer | using std::disclaimer;
Giage, Ltd. | http://www.giage.com
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Robert Klemme <robert.klemme@myview.de>
Date: 2000/04/13 Raw View
Fernando Luis Cacciola Carballal schrieb:
>
> Hi.
>
> I am participating in a thread about trimming whitespaces in a string on
> comp.lang.c++.moderated.
> I claimed that the call s.erase ( s.find_last_not_of ( t ) + 1 ) is always
> safe since basic_string::npos is defined to be 'the largest value of type
> size_t', which implies that npos+1 is guaranteed to be 0.
>
> However, reader Rob Stewart looked it up and didn't find a precise
> definition for npos that guarantees the above.
>
> Am I correct? Or a portable implementation should test for inequality to
> npos before calling erase()?
independant whether this is so or not i would not use such idioms. i
would not calculate with this special constant and just use it for
comparisons since this is used for reporting or announcement of special
conditions.
robert
--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra=DFe 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Fernando Luis Cacciola Carballal" <fcacciola@fibertel.com.ar>
Date: 2000/04/11 Raw View
Hi.
I am participating in a thread about trimming whitespaces in a string on
comp.lang.c++.moderated.
I claimed that the call s.erase ( s.find_last_not_of ( t ) + 1 ) is always
safe since basic_string::npos is defined to be 'the largest value of type
size_t', which implies that npos+1 is guaranteed to be 0.
However, reader Rob Stewart looked it up and didn't find a precise
definition for npos that guarantees the above.
Am I correct? Or a portable implementation should test for inequality to
npos before calling erase()?
Thanks.
(I don't have a copy of the IS so I can't look it up myself)
- --
Fernando Cacciola
Sierra s.r.l.
fcacciola@fibertel.com.ar
www.gosierra.com
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Matt Austern <austern@sgi.com>
Date: 2000/04/11 Raw View
"Fernando Luis Cacciola Carballal" <fcacciola@fibertel.com.ar> writes:
> Hi.
>
> I am participating in a thread about trimming whitespaces in a string on
> comp.lang.c++.moderated.
> I claimed that the call s.erase ( s.find_last_not_of ( t ) + 1 ) is always
> safe since basic_string::npos is defined to be 'the largest value of type
> size_t', which implies that npos+1 is guaranteed to be 0.
The basic_string<> synopsis, at the end of section 21.3, says that
npos is defined as
static const size_type npos = -1;.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]