By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

Is std::string::npos portably incrementable?

P: n/a
Is std::string::npos portably able to be incremented?

For example, I want to insert some text into a string. If a certain
character is found, I want to insert this text immediately after this
character; otherwise I insert at the beginning of the string. On my
implementation string::npos has the value of (string::size_type)-1, so
incrementing it will make it 0, but can I rely on it?

Also, say that the character occurs at the end of the string. Is it
valid to specify the insert position as one greater than this value?

Example usage of what I want to do is shown in fix_path(). Everything
behaves as I would expect on my implementation (VS 2005).
#include <iostream>
#include <string>

void fix_path(std::string& s)
{
using std::string;

string to_insert = "goes/";
string::size_type pos = s.rfind('/');
s.insert(pos + 1, to_insert);
}

void test(const std::string& s)
{
using std::cout;
using std::string;

string t(s);
cout << t << '\n';
fix_path(t);
cout << t << "\n\n";
}

int main()
{
using std::cout;
using std::string;

test("my/stuff/here");
test("here");
test("another/");
test("/beginning");
}
/*
Output:
my/stuff/here
my/stuff/goes/here

here
goes/here

another/
another/goes/

/beginning
/goes/beginning
*/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 29 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Marcus Kwok wrote:
Is std::string::npos portably able to be incremented?

For example, I want to insert some text into a string. If a certain
character is found, I want to insert this text immediately after this
character; otherwise I insert at the beginning of the string. On my
implementation string::npos has the value of (string::size_type)-1, so
incrementing it will make it 0, but can I rely on it?

Also, say that the character occurs at the end of the string. Is it
valid to specify the insert position as one greater than this value?

Example usage of what I want to do is shown in fix_path(). Everything
behaves as I would expect on my implementation (VS 2005).
#include <iostream>
#include <string>

void fix_path(std::string& s)
{
using std::string;

string to_insert = "goes/";
string::size_type pos = s.rfind('/');
s.insert(pos + 1, to_insert);
}

void test(const std::string& s)
{
using std::cout;
using std::string;

string t(s);
cout << t << '\n';
fix_path(t);
cout << t << "\n\n";
}

int main()
{
using std::cout;
using std::string;

test("my/stuff/here");
test("here");
test("another/");
test("/beginning");
}
/*
Output:
my/stuff/here
my/stuff/goes/here

here
goes/here

another/
another/goes/

/beginning
/goes/beginning
*/
Would boost::regex_replace be a better choice in this case?

Fei
Jun 29 '07 #2

P: n/a
Marcus Kwok wrote:
Is std::string::npos portably able to be incremented?
By definition, npos is 'size_type' and has the value -1. See 21.3/6.
For example, I want to insert some text into a string. If a certain
character is found, I want to insert this text immediately after this
character; otherwise I insert at the beginning of the string. On my
implementation string::npos has the value of (string::size_type)-1
It is required to be that on all implementations.
>, so
incrementing it will make it 0, but can I rely on it?
I believe so.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '07 #3

P: n/a
On Fri, 29 Jun 2007 15:14:34 +0000, Marcus Kwok wrote:
Is std::string::npos portably able to be incremented?

For example, I want to insert some text into a string. If a certain
character is found, I want to insert this text immediately after this
character; otherwise I insert at the beginning of the string. On my
implementation string::npos has the value of (string::size_type)-1, so
incrementing it will make it 0, but can I rely on it?
Most likely yes, since the standard defines 'npos' as:
static const size_type npos = -1;

And 'size_type' is:
typedef typename Allocator::size_type size_type;

--
Obnoxious User
Jun 29 '07 #4

P: n/a
Victor Bazarov <v.********@comacast.netwrote:
Marcus Kwok wrote:
>>, so
incrementing it will make it 0, but can I rely on it?

I believe so.
Thanks. Do know the answer to my other question (whether or not it is
well-defined to specify s.size() as the insert position for string)?

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 29 '07 #5

P: n/a
Marcus Kwok wrote:
Victor Bazarov <v.********@comacast.netwrote:
>Marcus Kwok wrote:
>>, so
incrementing it will make it 0, but can I rely on it?
I believe so.

Thanks. Do know the answer to my other question (whether or not it is
well-defined to specify s.size() as the insert position for string)?
It's well defined.

john
Jun 29 '07 #6

P: n/a
John Harrison <jo*************@hotmail.comwrote:
Marcus Kwok wrote:
>Thanks. Do know the answer to my other question (whether or not it is
well-defined to specify s.size() as the insert position for string)?

It's well defined.
Great, thanks.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 29 '07 #7

P: n/a
On Jun 29, 5:14 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Is std::string::npos portably able to be incremented?
The obvious answer is that it's a constant, and you can't
increment a constant. But from the rest of your post, I gather
that what you really want to know is whether npos + 1 is
guaranteed to be zero.

Technically, the answer is no. npos is guaranteed to have the
value (size_t)(-1), and size_t is guaranteed to be an unsigned
type, so the value would be guaranteed to be 0, unless integral
promotion occurs. However:

-- I've never heard of an implementation where size_t was
smaller than an unsigned int, so integral promotion won't
occur, and

-- even if integral promotion occurs, if you immediately
reconvert the results back to a size_t, you're guaranteed to
end up with 0.

So in practice, I think you can count on it.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 30 '07 #8

P: n/a
James Kanze <ja*********@gmail.comwrote:
On Jun 29, 5:14 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
-- even if integral promotion occurs, if you immediately
reconvert the results back to a size_t, you're guaranteed to
end up with 0.

So in practice, I think you can count on it.
Thanks for the confirmation. I am just using it as the parameter for
string::insert(), which is of size_type, so I should be fine.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 2 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.