wxArray size_t or int, which one is it?
wxArray size_t or int, which one is it?
When trying to fix a few of the signed/unsigned mismatches my compiler keeps reminding me about, I noticed a few oddities about the wxArray class.
Now, maybe these oddities were decision based, maybe not. If they are, then I hope someone can help me understand why these decisions were made.
As I understand, wxArray uses a size_t as index number. size_t is an unsigned integer (in this case), so it can store from 0 to 4,294,967,295. So when calling wxArray's GetCount() you ought to be comparing to another size_t.
But here's the strange thing. wxArray::Index(T &item) returns an INT. Theoretically from -2,147,483,648 to 2,147,483,647.
What happens when the item I'm looking for is item number 3,000,000,000 ?
So basically my question is: why does wxArray::Index(T &item) return an int, while wxArray::GetCount() returns a size_t?
Now, maybe these oddities were decision based, maybe not. If they are, then I hope someone can help me understand why these decisions were made.
As I understand, wxArray uses a size_t as index number. size_t is an unsigned integer (in this case), so it can store from 0 to 4,294,967,295. So when calling wxArray's GetCount() you ought to be comparing to another size_t.
But here's the strange thing. wxArray::Index(T &item) returns an INT. Theoretically from -2,147,483,648 to 2,147,483,647.
What happens when the item I'm looking for is item number 3,000,000,000 ?
So basically my question is: why does wxArray::Index(T &item) return an int, while wxArray::GetCount() returns a size_t?
-
- Knows some wx things
- Posts: 26
- Joined: Tue Nov 14, 2006 1:51 pm
- Location: Duesseldorf, Germany
Since I'm interested, too, this still doesn't work out for item no. 3000000 for example.
A solution could be defining the not_found-id as (size_t)-1 (or more conformant std::numeric_limits<size_t>::max()-1), which is the largest possible size_t. This would only render one possible index useless, not half the range...
A solution could be defining the not_found-id as (size_t)-1 (or more conformant std::numeric_limits<size_t>::max()-1), which is the largest possible size_t. This would only render one possible index useless, not half the range...
-
- Super wx Problem Solver
- Posts: 307
- Joined: Fri Oct 08, 2004 8:21 am
- Location: Area 51
- Contact:
I agree with this assessment and also with the proposed solution. I would recommend submitting a patch.S.Volkenandt wrote:A solution could be defining the not_found-id as (size_t)-1 (or more conformant std::numeric_limits<size_t>::max()-1), which is the largest possible size_t. This would only render one possible index useless, not half the range...
- Santiago
http://www.metalogicsw.com
http://www.metalogicsw.com
I don't think this is such a good idea. Lots of people come from Windows programming where unselected/not found = -1. I guess this will break lots of code as probably people often check for >=0 and not for !=wxNOT_FOUND.metalogic wrote:I agree with this assessment and also with the proposed solution. I would recommend submitting a patch.S.Volkenandt wrote:A solution could be defining the not_found-id as (size_t)-1 (or more conformant std::numeric_limits<size_t>::max()-1), which is the largest possible size_t. This would only render one possible index useless, not half the range...
... and besides, wx will use exclusively STL in the future in which there are no such "problems", wouldn't it ?Belgabor wrote: I don't think this is such a good idea. Lots of people come from Windows programming where unselected/not found = -1. I guess this will break lots of code as probably people often check for >=0 and not for !=wxNOT_FOUND.
-
- Knows some wx things
- Posts: 26
- Joined: Tue Nov 14, 2006 1:51 pm
- Location: Duesseldorf, Germany
Agreed, as long as dereferencing iterators of wxArray doesn't evaluate to void* (at least I've experienced that with 2.7, but I will verify it before filing any reports).jgeorgal wrote:... and besides, wx will use exclusively STL in the future in which there are no such "problems", wouldn't it ?
Are there any official (meaning online-readable) specs about what wx will use in future (i.e. 3.0)? Maybe even parts of std::tr1?