Search code examples
c++coperatorsdefined

defined(XXXX) macro in C/C++?


I'm going through the BSON source code, and came across something I've never seen before.

Line 22 in bson-macros.h:

#if !defined(BSON_INSIDE) && !defined(BSON_COMPILATION)
#error "Only <bson.h> can be included directly."
#endif

What is the defined(XXXX) macro above? I can guess what it does, but I can't seem to find any documentation about it. Is it specific to some compilers? It gives me a W4 warning on Microsoft Visual C++ (that I'm trying to resolve in my project).


Solution

  • From 6.10.1

    The expression that controls conditional inclusion shall be an integer constant expression except that: identifiers (including those lexically identical to keywords) are interpreted as described below;166) and it may contain unary operator expressions of the form

      defined identifier
    

    or

      defined ( identifier )
    

    which evaluate to 1 if the identifier is currently defined as a macro name (that is, if it is predefined or if it has been the subject of a #define preprocessing directive without an intervening #undef directive with the same subject identifier), 0 if it is not.

    It is not macro - it is an operator.