I want to define a macro if a condition involving sizeof
is true and do nothing (but still compile) if it is false. If the preprocessor supported sizeof
, it would look like this:
#if (sizeof(void*) <= sizeof(unsigned int)) // what goes here?
# define POINTER_FITS_INTO_UINT
#endif
There are some pages (e.g. http://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/) which explain how to make a compile-time assertion on sizeof
(and fail to compile if it fails), but I don't see a way to extend this approach to what I want.
You just can't do it. sizeof is a compile time operator. #if and #define and preprocessor related. As the preprocessor runs BEFORE the compiler this just won't work. You may, however, be able to find an arcane compiler switch that will allow you to multi pass it (ie preprocess, pretend compile, preprocess, compile) but, in all fairness, I'd give up trying to do what you want. Its not meant to work and, simply, it doesn't.
Your best best is to set such defines as -D commands passed to the compiler. You can statically assert that the ones chosen are correct. This way you just have to set up a few defines externally for a given compile mode (eg PowerPC Release) and so on.