Serve Laurijssen
Serve Laurijssen

Reputation: 9733

No error on incorrect macro definition

While replacing macros with constexpr auto I came across the following macro

#define MSG_SELECTED_INSPECTION [OF D] 490

This has been in the project forever and it compiles just fine.

Removing the space however

#define MSG_SELECTED_INSPECTION[OF D] 490

emits the error '[':unexpected macro definition

How come no error is emitted on the first line?

Upvotes: 0

Views: 96

Answers (3)

How come no error is emitted on the first line?

A macro has two forms, function-like and object-like (what you have).

#define CONST ...
#define FUNC(...) ...

A space is required as a separator between your macro and the sequence of tokens it's defined to be replaced by. When you removed the space, it stopped being a valid (object-like) macro definition.

Upvotes: 2

user743382
user743382

Reputation:

Macros are largely a text replacement facility. #define MSG_SELECTED_INSPECTION [OF D] 490 is a perfectly valid macro definition. Whenever MSG_SELECTED_INSPECTION is seen, it will be replaced by [OF D] 490.

The reason #define MSG_SELECTED_INSPECTION[OF D] 490 is because the definition must be separated from the macro name by at least a space. This is because not all compilers allow the same set of characters in identifiers, and it would be harmful if

#define A$B C

would define a macro A as expanding to $B C on one implementation, and a macro A$B as expanding to C on another.

Upvotes: 3

Jarod42
Jarod42

Reputation: 217275

Macro is text replacement.

So before, you replace MSG_SELECTED_INSPECTION by [OF D] 490. And it seems that you didn't use MSG_SELECTED_INSPECTION before.

Upvotes: 2

Related Questions