Thread View: comp.std.c++
2 messages
2 total messages
Started by Nico Josuttis
Tue, 02 Apr 1996 15:16
string::npos is unhandy and dangerous
Author: Nico Josuttis
Date: Tue, 02 Apr 1996 15:16
Date: Tue, 02 Apr 1996 15:16
50 lines
2135 bytes
2135 bytes
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.
I have some problems with that:
1.) If i understand it right I have to write:
string::size_type pos;
pos = s.find('x');
if (pos == string::npos)
// not found
...
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?
2.) I wonder what "npos" means.
Could somebody tell me?
--------
Nico address: BREDEX GmbH
email: nico@bredex.de Nicolai Josuttis
Fallersleber-Tor-Wall 23
phone: +49 531 24330-0 D-38100 Braunschweig
fax: +49 531 24330-99 Germany
--------
[ 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 ]
Re: string::npos is unhandy and dangerous
Author: 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 ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads