I came across some C code where the author uses the following idiom all over the place:
typedef __int32 FOO_INT32;
#define FOO_INT32 FOO_INT32
What is the point of doing this? Shouldn't the typedef be enough? It is a workaround for some wonky C compilers out there?
With the #define
instruction, you'll then be able to test if the typedef has been done somewhere else in the code using :
#ifdef FOO_INT32
FOO_INT32 myfoo;
#else
int myfoo;
#endif
It's a practice that's sometimes done in headers. The #define allows for compile-time testing of the existence of the typedef. This allows code like:
#ifdef FOO_INT32
FOO_INT32 myfoo;
#else
int myfoo;
#endif
or as a true guard #define, similar to header file guards.
#ifndef FOO_INT32
typedef int FOO_INT32
#define FOO_INT32 FOO_INT32
#endif
It's not necessarily a good practice, but it has its uses, especially when you have some headers which use types defined by other libraries, but you want to provide your own substitutes for cases when you're not using those libraries at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With