c++c-preprocessorsizeof

Checking the sizeof an integer type in the preprocessor


How can I check the size of an unsigned in the preprocessor under g++? sizeof is out of the question since it is not defined when during preprocessing.


Solution

  • This may not be the most elegant method, but one thing that you may be able to leverage is UINT_MAX defined in "limits.h". That is, ...

    if UINT_MAX == 65535, then you would know that sizeof (unsigned) = 2

    if UINT_MAX == 4294967295, then you would know that sizeof (unsigned) = 4.

    and so on.

    As I said, not elegant, but it should provide some level of usability.

    Hope this helps.