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

后端 未结 11 2237
太阳男子
太阳男子 2020-12-15 16:03

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

11条回答
  •  Happy的楠姐
    2020-12-15 16:32

    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.

提交回复
热议问题