|
|
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 |
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 #20228 is a reply to message #20224] |
Mon, 02 March 2009 10:15 |
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 |
|
mirek
Messages: 14039 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 #20256 is a reply to message #20224] |
Thu, 05 March 2009 12:29 |
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.
|
|
|
Goto Forum:
Current Time: Sat Sep 21 02:18:06 CEST 2024
Total time taken to generate the page: 0.00913 seconds
|
|
|