Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Library : Other (not classified elsewhere) » string[] causes many overload complaints
string[] causes many overload complaints [message #20224] Sun, 01 March 2009 18:17 Go to next message
aftershock is currently offline  aftershock
Messages: 143
Registered: May 2008
Experienced Member
I had a code:

unsigned int i=0;
String s;
if (s[i]==' ')
{

}

and Microsoft Compiler complaint a lot (cannot decide which
overload to use....


because of s[i]


My solution was to add:

int operator[](unsigned int i) const { ASSERT(i >= 0 && i <= B::GetCount()); return B::Begin()[i]; }

line in String.h





Re: string[] causes many overload complaints [message #20225 is a reply to message #20224] Mon, 02 March 2009 08:44 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Why do not you just use 'int'?!

Mirek
Re: string[] causes many overload complaints [message #20226 is a reply to message #20224] Mon, 02 March 2009 08:47 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
I prefer personally unsigned as index variable, because it makes it easy to check for negative indexes. Wink
Re: string[] causes many overload complaints [message #20227 is a reply to message #20226] Mon, 02 March 2009 09:06 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Strange. Could you please explain how checking negative for unsigned int is better than using signed? As far as I understand, you check one bit? So what makes you think that (i == -1) is quicker than (i & 0x80000000)?

[Updated on: Mon, 02 March 2009 09:08]

Report message to a moderator

Re: string[] causes many overload complaints [message #20228 is a reply to message #20224] Mon, 02 March 2009 10:15 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
Uhm, I was sort of joking in a cryptic way, so to make myself clear.
If you need a range from 0 to MaxN, and you have index I, with "signed" type you need to validate I by:
is_valid = ( 0 <= I && I <= MaxN );

With "unsigned" type for I the very same condition turns into:
is_valid = (I <= MaxN);

This is why I like to use 0..N ranges indexed by unsigned variables, the safety checks then cost me just single compare, not two of them.

Addendum:
And AFAIK there's no special use for negative index values in NTL containers, ranges are always from 0, so if I would design the NTL containers, the basic [] operator would work with unsigned type, not signed. Probably breaking the convention of many programmers writing "for (int i=0; i<max; i++)" ... I never care too much about conventions anyway, unless they make sense, and this one does not.

[Updated on: Mon, 02 March 2009 10:21]

Report message to a moderator

Re: string[] causes many overload complaints [message #20234 is a reply to message #20228] Mon, 02 March 2009 16:40 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mr_ped wrote on Mon, 02 March 2009 04:15


And AFAIK there's no special use for negative index values in NTL containers, ranges are always from 0, so if I would design the NTL containers, the basic [] operator would work with unsigned type, not signed. Probably breaking the convention of many programmers writing "for (int i=0; i<max; i++)" ... I never care too much about conventions anyway, unless they make sense, and this one does not.


Relative offsets can be negative. Obviously, in the end it always ends as positive number, but values involved in computing the index value can be negative. Using 'int' may avoid some casting (or equivalent warnings).

Mirek
Re: string[] causes many overload complaints [message #20237 is a reply to message #20224] Mon, 02 March 2009 23:37 Go to previous messageGo to next message
aftershock is currently offline  aftershock
Messages: 143
Registered: May 2008
Experienced Member
funny
I got a lot of error because of that.


unsigned int could be about type safety.
Re: string[] causes many overload complaints [message #20239 is a reply to message #20237] Tue, 03 March 2009 08:20 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
aftershock wrote on Mon, 02 March 2009 17:37

funny
I got a lot of error because of that.


unsigned int could be about type safety.



unsigned int index1, index2;
int offset = index1 - index2;


Mirek
Re: string[] causes many overload complaints [message #20242 is a reply to message #20239] Wed, 04 March 2009 12:07 Go to previous messageGo to next message
aftershock is currently offline  aftershock
Messages: 143
Registered: May 2008
Experienced Member
Could you explain what you wrote in a more detailed way?

Why did you write that?
What are you trying to show?

[Updated on: Wed, 04 March 2009 12:08]

Report message to a moderator

Re: string[] causes many overload complaints [message #20243 is a reply to message #20224] Wed, 04 March 2009 16:48 Go to previous messageGo to next message
Infausto is currently offline  Infausto
Messages: 28
Registered: June 2008
Promising Member
i suppose mirek wrote that to demonstrate you than an index can be negative when you calculate an offset.

salute.
Re: string[] causes many overload complaints [message #20253 is a reply to message #20224] Thu, 05 March 2009 12:09 Go to previous messageGo to next message
aftershock is currently offline  aftershock
Messages: 143
Registered: May 2008
Experienced Member
Ok,

I do not know why would anyone need an offset.
Personally, I do not subtract indecies.

Even if you do, I guess you do not put them into an a[].
If you do, how often does one need an operation like that?
I almost never.
For that reason, to make the type of indices int seems to me an overkill.


Anyway, there could be a definition for both type of indices....
with int and unsigned type....
STL uses unsigned types.
Where can you see a problem with that?


Re: string[] causes many overload complaints [message #20256 is a reply to message #20224] Thu, 05 March 2009 12:29 Go to previous message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
I can imagine offsets needed, but:
1) I rarely need them
2) they work well even when they are declared as unsigned, and it doesn't hurt my mind to do things like index += unsigned(-1);, actually it hurts much less then having to do NTL[int(unsigned_index)] every time I have to work with container value.

Quote:

Where can you see a problem with that?


It's purely matter of taste?
Except saving 1 compare when boundary checking with unsigned, and having 2*max_range (both of them I like much more then signed offsets), I don't see any major difference between those two, so I never really bothered and I can see why Mirek insists on his way.
I'm just adding the int(..) casting when the compiler yells, for me personally it's not even worth to add unsigned variants into NTL, because I don't run into thing that often.
Previous Topic: Linking problem with socket
Next Topic: n-State Button
Goto Forum:
  


Current Time: Fri Mar 29 15:48:42 CET 2024

Total time taken to generate the page: 0.01918 seconds