🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3890

string::npos is unhandy and dangerous

#3890
From: Nico Josuttis
Date: Tue, 02 Apr 1996 15:16
50 lines
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                             ]
Technical Details
Message-ID: <199604020902.LAA06787@bredex.bredex.de>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: Nico Josuttis <nico@bredex.de>
Newsgroups: comp.std.c++
Subject: string::npos is unhandy and dangerous
Date: 2 Apr 1996 15:16:07 GMT
Organization: ?
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <199604020902.LAA06787@bredex.bredex.de>
NNTP-Posting-Host: taumet.eng.sun.com
X-Authentication-Warning: bredex.bredex.de: Host localhost didn't use HELO protocol
X-Mts: smtp
Content-Length: 1196
Originator: clamage@taumet