Saturday, May 23, 2009

LP64 and -Wshorten-64-to-32

Along with new Mac OS-X 64-bit makefiles for FastFormat and Pantheios, I'm also playing around with the -Wshorten-64-to-32 warning flag.

In compiling FF with this I encountered a lot of similars, amounting to the following:

enum { x = sizeof(Y) };

Obviously an enum, being int in size, is too small to hold size_t (the result of sizeof operator). The ugly but effective solution to this is:

enum { x = int(sizeof(Y)) };

which you'll now see more of in the STLSoft libs.

1 comment:

Jeff Walden said...

You're behind the times. C specified enums as effectively synonyms of int, but C++ specifies enums as more or less having indeterminate size, except that they're sized large enough to fit all values of their possible constants. See also:

If the problem you describe is occurring in code compiled as C++, that's a problem in the compiler, at least so long as size_t is the same type as unsigned int or unsigned long int (the smaller versions would also be fine, but their use seems unlikely for such these days; also, unsigned long long is technically non-standard and so doesn't actually fit). See 3.9.1 paragraphs 3 and 7 and 7.2 paragraph 5 of ISO C++ if you can get access to a copy anywhere.