c++c-preprocessor

Is this parenthesis-free C preprocessor definition safe?


In my /usr/include directory, there are at least two variants of #define NULL 0 tailored for C++ code1:

#define NULL 0    // from rpc/types.h
#define NULL (0)  // from libio.h

I feel like there must be a counter-example where the first one would not be safe, but I was not able to produce it.

Otherwise, is there some compelling argument about why it would be safe to not include the parentheses in this case (e.g., an informal "proof of correctness")?


1 That is, without including the variant #define NULL ((void*)0), which is useful for C but invalid in C++.


Solution

  • There isn't any difference. The only operators with higher priority are ::, ++ and -- and they are not applicable on 0 nor (0).

    The only funny difference I see is obfuscation:

    #define NULL (0)
    
    void f(int x)
    {
      // Do something with x
    }
    
    int main()
    {
      f NULL; // This code compiles
      return 0;
    }