αλεχολυτ
αλεχολυτ

Reputation: 5050

Function-like macros without body but different names of argument

The following code:

#define MYDEF(x)
#define MYDEF(y)
int main() {}

gives me an error (or warning if pedantic-errors is disabled):

'MYDEF' macro redefined

The reason is different names for unused argument (more over, there is no body in macro). But why? In which situations it can be a problem?

Upvotes: 0

Views: 161

Answers (2)

Joe
Joe

Reputation: 410

The macro can be redefined, and the macro is uniquely determined by the macro name. For example, code like this :

#define MYDEF(x)  //the name of the macro is 'MYDEF'
#define MYDEF(x, y) //the name of the macro is 'MYDEF' too

MYDEF(x) will be redifined(or covered) by MYDEF(x, y), you can't write code MYDEF(x) any more after defining MYDEF(x, y)

so, if you write code :

 #define MYDEF(x)
 #define MYDEF(y) //(There compiler will give warning). You can write 
                  //`#undef MYDEF` before `#define MYDEF(y)` to avoid it.

MYDEF(x) will be redifined by MYDEF(y).

Upvotes: 0

SergeyA
SergeyA

Reputation: 62613

Because macros are not functions. They are textual replacements done by the preprocessor and can't be overloaded.

It is (almost) similar to find and replace in your editor. Find all the occurences of MYDEF and replace it with (empty string in your case). It's more complicated, of course, but the idea is the same.

And you can't overload this find and replace, can you? :)

Upvotes: 7

Related Questions