typedef a primitive type

Started by
18 comments, last by rnlf_in_space 10 years, 11 months ago

I think that thinking of "cross-platform" with these typedefs is only half the story, and possibly even the less common reason. It is of course indeed the case with the "precise size" typedefs in stdint.h.

For the most part, the like typedefs, are much more about code maintainability and making code forward compatible, not so much about "cross-platform".

Cross-platform, is something that you can give a crap about, 95% of the time. Unless you have to layout a structure so it exactly fits some given structure or you make assumptions such as sizeof(int) == sizeof(void*), you usually couldn't care less about portability. An integer is an integer, and most of the time there's no noticeable difference (overflow due to an odd too-small-size integer type is most unlikely nowadays, this is not the 1970s any more).

On the other hand, consider that you wrote 200,000 lines of code that assume some function (let's say posix_fadvise, or glBufferData) takes an integer argument (or, maybe even worse, returns one: say CreateWindow). Incidentially, your integers are 32bit values, but that's big enough for everyone, isn't it. Integers are the same size as a pointer, too, how lucky is that!

One day the maintainers of that function realize that hard disks can be quite a bit larger than 4GiB these days, and people actually have files larger than 4GiB, too (or vertex buffers >4GiB). So they change the function to take a 64bit integer. That's 200,000 lines of code down the drain for you. The maintainers of the other function realize that integers are not necessarily the same size as pointers (did someone say SetWindowLong?).

Now, the clever maintainer would have used off_t for posix_fadvise, and HWND for CreateWindow, which does not look like there is much of an advantage. Indeed it actually looks quite stupid, so many different types for only the same thing.

However, if one day, it is decided that one has to change the typedef, your 200,000 lines of code just need to be recompiled once, and they will work. No manual editing every occurrence needed.

This topic is closed to new replies.

Advertisement