Alex Rivera | Logout

In C/C++, is there a directive similar to #ifndef for typedefs?

Asked 2011-07-21T07:44:12.517
58

If I want to define a value only if it is not defined, I do something like this :

#ifndef THING
#define THING OTHER_THING
#endif

What if THING is a typedef'd identifier, and not defined? I would like to do something like this:

#ifntypedef thing_type
typedef uint32_t thing_type
#endif

The issue arose because I wanted to check to see if an external library has already defined the boolean type, but I'd be open to hearing a more general solution.

c++ c
Edit
Report

1 Answer

23

C++ does not provide any mechanism for code to test presence of typedef, the best you can have is something like this:

#ifndef THING_TYPE_DEFINED
#define THING_TYPE_DEFINED
typedef uint32_t thing_type 
#endif

EDIT:
As @David, is correct in his comment, this answers the how? part but importantly misses the why? It can be done in the way above, If you want to do it et all, but important it you probably don't need to do it anyways, @David's answer & comment explains the details, and I think that answers the question correctly.

answered 2011-07-21T07:52:50.970

Your Answer