Re: Unsignedness of std::size_t
On Apr 12, 11:06 pm, "Lance Diduck" <lancedid...@nyc.rr.com> wrote:
On Apr 11, 7:44 pm, Kaba <REkalleMOunderscoreVErutane...@hotmail.com>
wrote:> Here you can find an article "Signed and unsigned types in
interfaces"
by Scott Meyers:
http://www.aristeia.com/publications_frames.html
After reading that article it should be clear why it is not a good idea
to use unsigned types just to reinforce a positiveness precondition to a
function parameter. Yet the standard library does just this by using
std::size_t which is unsigned.
This article is from the year 1995, three years before the 1998
standard. Why is the std::size_t still unsigned?
A little higher on the same page you will see "Interface types
revisited (and more)," Give that a read too.
Thanks! The first article is quite misleading, IMHO. The second one
is helpful and gives some insight.
However, as for me there is some little more confusion about size_t I
would like to get rid of. I regularly have somewhat bad feeling when
I write the for-loops like this:
size_t get_size();
size_t n = get_size();
for (int i = 0; i < n; ++i)
...
Compilers I use (g++ and MSVC++) are correctly issue warnings about
comparison of singed and unsigned integers in such cases. In general,
I find myself tending to ignore such kind of warnings since most of
the time I know from some preconditions what `n' will never exceed
MAX_INT. But still I doubt this is a correct way to handle this. :-)
One way might be to declare `i' size_t, but I find this misleading
since `i' here is not a size but rather an index of some sort.
Another one is to declare `i' unsigned, but what if later I need to
reverse the index order? I could find myself writing such code then:
for (unsigned i = get_size(); --i >= 0; )
...
Gotcha! Now this loop is never going to end. :-)
Yeah, it might be possible to handle the case of i==0 specially:
for (unsigned i = get_size(); --i > 0; )
...
// do the job for i==0
But I, personally, hate special cases. Do you? It might be possible
to forget to put that special line too...
So is there a way to treat both of the loops (straight and reversed)
uniformly and to avoid the warning?
Yet another source of confusion comes not from the loops but, for
example, integer geometry calculations. There might be lots of
algorithms where you have to deal with coordinates (x,y--signed) and
dimensions (width, height, length or radius--unsigned) as input
parameters. A typical algorithm dictates to combine coordinates and
dimensions in a certain way in order to obtain the result. Every now
and when such a formula involves comparison of singed and unsigned
values you get a warning...
I feel there should be a way to systematically avoid such warnings,
but, regretfully, didn't bother find it out myself yet. If anyone
have some experience in it I would like to prompt you to share it.
Hope, this is not too much off topic. :-)
--
Alex Shulgin
PS: in no event this post should be treated as an argument to change
std::size_t to signed int. :-)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]