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

后端 未结 11 2240
太阳男子
太阳男子 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条回答
  •  借酒劲吻你
    2020-12-15 16:28

    No there is nothing like what you wanted. I have had your same problem with libraries that include their owntypedefs for things like bool. It gets to be a problem when they just don't care about what you use for bool or if any other libs might be doing the same thing!!

    So here's what I do. I edit the header file for the libs that do such things and find the typedef bool and add some code like this:

    #ifdef USE_LIBNAME_BOOL
    typedef unsigned char bool; // This is the lib's bool implementation
    #else
    #include 
    #endif
    

    Notice that I included if I didn't want to use the libs' own bool typdef. This means that you need C99 support or later.

提交回复
热议问题