String trimming throwing an exception

A

Adrian

Can this code ever throw out_of_range or should the commented out code
always be used?

Is there any chance that std::string::npos+1 is bigger then
std::string::npos

Take str to be any allowable string value.

#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
std::string str;

str.erase(0, str.find_first_not_of(" \t"));
str.erase(str.find_last_not_of(" \t")+1, std::string::npos);

/*
str.erase(0, str.find_first_not_of(" \t"));
std::string::size_type x=str.find_last_not_of(" \t");
if(x!=std::string::npos)
{
str.erase(x+1, std::string::npos);
}
*/
return 0;
}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Can this code ever throw out_of_range or should the commented out code
always be used?

Is there any chance that std::string::npos+1 is bigger then
std::string::npos

npos is of type size_type and have the value -1, from what I can
determine size_type is an unsigned type meaning that npos is the
largest possible value that a variable of size_type can have. This
means that npos + 1 can never be larger than npos.
Take str to be any allowable string value.

#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
std::string str;

str.erase(0, str.find_first_not_of(" \t"));
str.erase(str.find_last_not_of(" \t")+1, std::string::npos);

So this will remove all but the first character if there are no tabs,
you should probably use the code below.
 
A

Adrian

npos is of type size_type and have the value -1, from what I can
determine size_type is an unsigned type meaning that npos is the
largest possible value that a variable of size_type can have. This
means that npos + 1 can never be larger than npos.

So on a standard implementaion it will never throw
So this will remove all but the first character if there are no tabs,
you should probably use the code below.
The both remove the same about of white space :) check again.
 
J

James Kanze

Can this code ever throw out_of_range or should the commented out code
always be used?
Is there any chance that std::string::npos+1 is bigger then
std::string::npos

Technically, I think so, but only if size_t is smaller than an
int (and integral promotion means that the expression has a type
larger than size_t). So I can't imagine an implementation where
it would happen in practice. And of course, if the npos+1 is
assigned to a size_t, it is guaranteed to be 0.
Take str to be any allowable string value.
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
std::string str;
str.erase(0, str.find_first_not_of(" \t"));
str.erase(str.find_last_not_of(" \t")+1, std::string::npos);

Since the parameter type of erase, here, is size_t, you should
be OK in every case.
 
J

James Kanze

On Apr 11, 12:30 am, "Erik Wikström" <[email protected]>
wrote:
So on a standard implementaion it will never throw

That's an interesting question, since the standard allows a lot
of variance in implementations. I think that it could result in
undefined behavior (and thus, anything, including throwing), but
it would take a very, very strange implementation, which I don't
think we'll ever see.

Basically, the only time it could cause trouble is if INT_MAX ==
USHRT_MAX and size_t were a typedef for unsigned short. Both
conditions are, I think, permitted, but I can't imagine ever
seeing them.

Quite frankly, I wouldn't worry about this possibility.
 
A

Adrian

That's an interesting question, since the standard allows a lot
of variance in implementations. I think that it could result in
undefined behavior (and thus, anything, including throwing), but
it would take a very, very strange implementation, which I don't
think we'll ever see.

Basically, the only time it could cause trouble is if INT_MAX ==
USHRT_MAX and size_t were a typedef for unsigned short. Both
conditions are, I think, permitted, but I can't imagine ever
seeing them.

Quite frankly, I wouldn't worry about this possibility.
Thanks James, that makes sense to me.

I never did worry about this :) until someone else questioned it so
thought I should check.

Adrian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top