Article View: comp.std.c++
Article #3918Re: string::npos is unhandy and dangerous
From: Nat Pryce
Date: Thu, 04 Apr 1996 23:45
Date: Thu, 04 Apr 1996 23:45
63 lines
3121 bytes
3121 bytes
In article <199604020902.LAA06787@bredex.bredex.de>,
Nico Josuttis <nico@bredex.de> writes:
>
>Following the standard, all find() member functions for string
>return string::size_type and may have the value string::npos,
>which is (size_type)-1.
... example elided...
> This seems unhandy und thus dangerous as people might
> try to make the code simpler:
> For example:
> int pos
> pos = s.find('x');
> if (pos == string::npos)
> or even
> if (pos == -1)
>
> The problem is that the test with operator== fails if
> sizeof(size_type) != sizeof(int)
> because unfortunately then
> (size_type)-1 is not -1
> This is due to the conversion to unsigned.
>
> Am I missing something or are these problems known?
> Why wasn't taken a simpler and safer interface?
Obviously these problems are known, because that is why the type
string::size_type and the constant string::npos were defined. This
is the safe interface, and I don't think it can be any simpler.
In fact I feel it is easier to read than "if( pos == -1 )" and
hides the implementation of the string. Is it really a problem to
type 14 extra characters, expecially when the result is code
which is more readable, easier to maintain, and *works*?
Basically, if you don't write to the interface then you cannot
expect your code to work in all cases. That is the point of an
interface, after all.
>
> 2.) I wonder what "npos" means.
> Could somebody tell me?
>
How about "no position"?
Cheers,
Nat.
--
+-------------------------------------------+--------------------------------+
| Name: Nat Pryce MEng ACGI O- | Mail: Department of Computing,|
| Email: np2@doc.ic.ac.uk | Imperial College, |
| Tel: +44 (0)171 594 8394 (Direct Dial) | 180 Queen's Gate, |
| Fax: +44 (0)171 581 8024 | London SW7 2BZ, |
| WWW: http://www-dse.doc.ic.ac.uk/~np2 | United Kingdom |
+-------------------------------------------+--------------------------------+
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<4k06dh$kni@penguin.doc.ic.ac.uk>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<199604020902.LAA06787@bredex.bredex.de>Show Full Headers
Complete Article Headers
From: np2@doc.ic.ac.uk (Nat Pryce) Message-ID: <4k06dh$kni@penguin.doc.ic.ac.uk> X-Original-Date: 4 Apr 1996 09:57:05 GMT Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 04 Apr 96 23:45:38 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: string::npos is unhandy and dangerous Organization: Dept. of Computing, Imperial College, University of London, UK. References: <199604020902.LAA06787@bredex.bredex.de> X-Newsreader: knews 0.9.4 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMWRfReEDnX0m9pzZAQFhaAF8CQUQVZdHd7T4/KX8tsNym+raE0HlW1nX x8Bvpmayht/JJZ2kOb6jYYG95htfsbdo =Xt2p