tags:

views:

387

answers:

2

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?

+21  A: 

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
claferri
+2  A: 

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.

SPWorley
same answer as first one ...
I guess if there were #ifndef FOO_INT32 just before the typedef, he would have guess the use as guards.