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

后端 未结 11 2214
太阳男子
太阳男子 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:10

    The solution I ended up using was including stdbool.h. I know this doesn't solve the question of how to check if a typedef is already defined, but it does let me ensure that the boolean type is defined.

    0 讨论(0)
  • 2020-12-15 16:14

    There is no such thing in the language, nor is it needed. Within a single project you should not have the same typedef alias referring to different types ever, as that is a violation of the ODR, and if you are going to create the same alias for the same type then just do it. The language allows you to perform the same typedef as many times as you wish and will usually catch that particular ODR (within the same translation unit):

    typedef int myint;
    typedef int myint;       // OK: myint is still an alias to int
    //typedef double myint;  // Error: myint already defined as alias to int
    

    If what you are intending to do is implementing a piece of functionality for different types by using a typedef to determine which to use, then you should be looking at templates rather than typedefs.

    0 讨论(0)
  • 2020-12-15 16:14

    The problem is actually real PITA, because some APIs or SDKs redefine commonly used things. I had issue that header files for a map processing software (GIS) were redefining TRUE and FALSE (generally used by windows SDK)keywords to integer literals instead of true and false keywords ( obviously, that can break SOMETHING). And yes, famous joke "#define true false" is relevant.

    define would never feel a typedef or constant declared in C\C++ code because preprocessor doesn't analyze code, it only scans for # statements. And it modifies code prior to giving it to syntax analyzer. SO, in general, it's not possible.

    https://msdn.microsoft.com/en-us/library/5xkf423c.aspx?f=255&MSPPError=-2147217396 That one isn't portable so far, though there were known request to implement it in GCC. I think, it also counts as "extension" in MSVC. It's a compiler statement, not a preprocessor statement, so it will not "feel" defined macros, it would detect only typedefs outside of function body. "full type" there means that it will react on full definition, ignoring statements like "class SomeClass;". Use it at own risk.

    Edit: apparently it also supported on MacOS now and by Intel comiler with -fms-dialect flag (AIX\Linux?)

    0 讨论(0)
  • 2020-12-15 16:15

    No there is no such facility in C++ at preprocessing stage. At the max can do is

    #ifndef thing_type
    #define thing_type uint32_t 
    #endif
    

    Though this is not a good coding practice and I don't suggest it.

    0 讨论(0)
  • 2020-12-15 16:18

    It is not transparent but you can try to compile it one time without typedef (just using the alias), and see if it compiles or not.

    0 讨论(0)
  • 2020-12-15 16:22

    This might not directly answer the question, but serve as a possible solution to your problem.

    Why not try something like this?

    #define DEFAULT_TYPE int // just for argument's sake
    #ifndef MY_COOL_TYPE
         #define MY_COOL_TYPE DEFAULT_TYPE
    #endif
    typedef MY_COOL_TYPE My_Cool_Datatype_t;
    

    Then if you want to customize the type, you can either define MY_COOL_TYPE somewhere above this (like in a "configure" header that is included at the top of this header) or pass it as a command line argument when compiling (as far as I know you can do this with GCC and LLVM, maybe others, too).

    0 讨论(0)
提交回复
热议问题