Counts as ints and size_t
- ABX
- Can't get richer than this
- Posts: 810
- Joined: Mon Sep 06, 2004 1:43 pm
- Location: Poznan, Poland
- Contact:
Counts as ints and size_t
Hi!
I'm looking for the feedback how user projects could be affected or what problems do you expect from changing counts in wxWidgets 2.8 api (and during 2.7 development time) from 'int' to 'size_t'. The idea is to make values which are unsigned in nature also unsigned in type. The question is whether such change does not add extra unneeded and unexpected work for users who maintain thousands lines of sources. Past measurement says that signed/unsigned warnings (while appears) are not big problem however there is known new warning about conversion between types on 64-bit platforms. Do you expect any other problems? Do you see benefits?
Thanks in advance for the feedback!
ABX
I'm looking for the feedback how user projects could be affected or what problems do you expect from changing counts in wxWidgets 2.8 api (and during 2.7 development time) from 'int' to 'size_t'. The idea is to make values which are unsigned in nature also unsigned in type. The question is whether such change does not add extra unneeded and unexpected work for users who maintain thousands lines of sources. Past measurement says that signed/unsigned warnings (while appears) are not big problem however there is known new warning about conversion between types on 64-bit platforms. Do you expect any other problems? Do you see benefits?
Thanks in advance for the feedback!
ABX
CVS Head, 2.8.X
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2
I voted 'yes'. I don't think there'd be big problems with that. Especialy if it's about counter values, it makes perfect sense to have them unsigned.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
-
- wxWorld Domination!
- Posts: 1339
- Joined: Wed Aug 03, 2005 8:10 am
- Location: BANGALORE, INDIA
- Contact:
I voted yes too.
I agree with upCASE and the idea behind the move, that values which are unsigned in nature also unsigned in type.
I agree with upCASE and the idea behind the move, that values which are unsigned in nature also unsigned in type.
Follow the development of my screenplay authoring program at http://wxscreenplaywriter.blogspot.com/
I voted yes, but have a side note. If a count value is also used as a status value, I find the construction (size_t)-1 to return an error less elegant. I do not know if wxWidgets also uses this where max(size_t) - 1 is always reserved for an error (must be somewhere in the wxDir part) .. ok I found it:
- Jorgen
However I do not know how that could be made more elegant, it is less intuitive then passing a < 0 value to represent an error.wxDir::Traverse
size_t Traverse(wxDirTraverser& sink, const wxString& filespec = wxEmptyString, int flags = wxDIR_DEFAULT)
Enumerate all files and directories under the given directory recursively calling the element of the provided wxDirTraverser object for each of them.
More precisely, the function will really recurse into subdirectories if flags contains wxDIR_DIRS flag. It will ignore the files (but still possibly recurse into subdirectories) if wxDIR_FILES flag is given.
For each found directory, sink.OnDir() is called and sink.OnFile() is called for every file. Depending on the return value, the enumeration may continue or stop.
The function returns the total number of files found or (size_t)-1 on error.
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
So do we make a wx::npos too? Would be logical to do that instead of (size_t)-1 at least the typing is easier.
- Jorgen
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
If size_t will be used as the type for sizes and counters then it is definitely necessary to have something like wx::npos.Jorg wrote:So do we make a wx::npos too? Would be logical to do that instead of (size_t)-1 at least the typing is easier.
Let me illustrate this with a simple example:
Code: Select all
std::string str = "abc";
if ( str.find( ".." ) != (unsigned int)-1 )
{
return 0;
}
Astonishingly the statement return 0 will be executed on a 64 bit machine, although the string does not contain "..".
std::string::find() returns the constant std::string::npos if the argument is not found within the string. You may say, but this is the largest unsigned integer, which is the same as -1 on all two's complement architectures, where is the problem?
The length of a std::string is of type size_type, which is usually the same as size_t. On a 64 bit machine this is equivalent to unsigned long, while unsigned int is a 32 bit value. The latter will be extended to 64 bit for the comparison, respecting the sign. Since it is unsigned after the cast, the comparison will use the value 0x00000000FFFFFFFF, but std::string::npos equals 0xFFFFFFFFFFFFFFFF.
On a 32 bit machine you will never realise the difference, but on a 64 bit machine you will.
Using the constant std::string::npos eliminates the problem.
You may argue, I could have used (size_t)-1 instead of (unsigned int)-1 to eliminate the problem. True, but then the code would break some day in the future when there will be a 128 bit architecture.
Regards,
Ulrich
-
- Super wx Problem Solver
- Posts: 307
- Joined: Fri Oct 08, 2004 8:21 am
- Location: Area 51
- Contact:
I voted yes and I also "vote" for a wx::npos.
Last edited by metalogic on Mon Mar 20, 2006 5:51 am, edited 1 time in total.
- Santiago
http://www.metalogicsw.com
http://www.metalogicsw.com
size_t is the most sensible way to handle counting. I also think a wx::npos constant should be introduced to handle errors accordingly to the change and further improve compatibility with the STL.
Back, just not as often
Windows Vista SP1, Ubuntu 8.04
MinGW 3.4.5, GCC 4.2.1
Code::Blocks
wxWidgets 2.8.9
Windows Vista SP1, Ubuntu 8.04
MinGW 3.4.5, GCC 4.2.1
Code::Blocks
wxWidgets 2.8.9
- Ryan Norton
- Moderator
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Well could be. But I see npos more as a general index offset, and string::npos was only an example. So I think wx::npos would be better.
- Jorgen
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb