c++c-preprocessor

Meaning of #undef in C++


I know what this means

#define M(B) (1U << ((sizeof(x) * CHAR_BIT) - B)) // CHAR_BIT=bits/byte

but I don't understand this one well:

#undef M

After this, what happens? Is M cleared, deleted, or?


Solution

  • After the #undef, it's as if the #define M... line never existed.

    int a = M(123); // error, M is undefined
    
    #define M(B) (1U << ((sizeof(x) * CHAR_BIT) - B))
    
    int b = M(123); // no error, M is defined
    
    #undef M
    
    int c = M(123); // error, M is undefined