#Undef value in C ++

I know what that means

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

but I do not understand this:

#undef M 

after that, what happens? M is cleared or removed or?

+3
source share
3 answers

After #undef, as if the line #define M...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
+14
source

Here is an MSDN article about it: http://msdn.microsoft.com/en-us/library/ts4w8783(VS.80).aspx

I understand that it removes the definition of M so that it can be used to define something else.

eg.

#define M(X) 2*(X)
int a = M(2); 
ASSERT(a == 4);
#undefine M
#define M(X) 3*(X)
int b = M(2);
ASSERT(b == 6);

This seems like a confusing thing, but it can come up in practice if you need to work with someone else's macro.

+2
source

#define #undef - .

.  #define M (X) 2 * (X)

(1);

#undef M

(2)

Since these are preprocessor directives before compilation, the preprocessor will simply be replaced after #define M (X) 2 * (X) in this source file.

M (1) s 2 * 1

if the preprocessor finds #undef M, it will no longer be replaced

M (2) with 2 * 2, because M is destroyed when #undef M.

#undef is used if you want to give a different definition for an existing macro

+1
source

Source: https://habr.com/ru/post/1753653/


All Articles