Counts as ints and size_t

This forum is reserved for everything you want to talk about. It could be about programming, opinions, open source programs, development in general, or just cool stuff to share!
Post Reply

Would you accept counts in wxWidgets 2.8 changed from 'int' to 'size_t'

yes
27
87%
no
1
3%
I don't care
3
10%
 
Total votes: 31

User avatar
ABX
Can't get richer than this
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

Post by ABX » Fri Mar 17, 2006 9:04 am

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
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

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Fri Mar 17, 2006 9:15 am

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

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Fri Mar 17, 2006 9:16 am

The warnings can be suppressed, but the standards cannot.
It gives a lot of warnings too for me, when wxWidgets try to convert size_t to int, without casting, I guess I have posted a bug too. So its better that what is size_t should be that only and not int or other.

NinjaNL
Moderator
Moderator
Posts: 899
Joined: Sun Oct 03, 2004 10:33 am
Location: Oosterwolde, Netherlands

Post by NinjaNL » Fri Mar 17, 2006 9:40 am

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.
Follow the development of my screenplay authoring program at http://wxscreenplaywriter.blogspot.com/

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Fri Mar 17, 2006 9:47 am

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:
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.
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.

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Fri Mar 17, 2006 10:38 am

I voted yes too, because I'm using size_t all over the place and think it is the only right type for a counting variable.

For the -1 case Jorg described. I have no problem with it. Even the C++ Standardlib is doing it (eg std::basic_string<>::npos), so I'm fine with it.

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Fri Mar 17, 2006 11:18 am

I voted for yes. I think it should be used, cause its almost a standard in a lot of modern C++ librarys.
afaik boost uses it too.

Still i find it unpractically, to test about std::string::npos instead of -1.

phlox

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Fri Mar 17, 2006 11:50 am

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
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

utelle
Moderator
Moderator
Posts: 1010
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Post by utelle » Fri Mar 17, 2006 4:20 pm

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.
If size_t will be used as the type for sizes and counters then it is definitely necessary to have something like wx::npos.

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

metalogic
Super wx Problem Solver
Super wx Problem Solver
Posts: 307
Joined: Fri Oct 08, 2004 8:21 am
Location: Area 51
Contact:

Post by metalogic » Sat Mar 18, 2006 4:46 am

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.

AkiraDev
Knows some wx things
Knows some wx things
Posts: 48
Joined: Tue May 24, 2005 9:13 am

Post by AkiraDev » Sat Mar 18, 2006 10:43 pm

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

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Sun Mar 19, 2006 12:47 am

hm, wx::npos.
Interesting, is there gonna be a namespace wx ?
Would make sense imho.

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Sat Apr 01, 2006 5:15 am

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.

- Jorgen
You mean wxString::npos? :)
[Mostly retired moderator, still check in to clean up some stuff]

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Sat Apr 01, 2006 7:24 am

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
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

Post Reply