There is no error that occurs specifically to cause a conflict. What happens is that macro processing is performed first, so namespaces are not taken into account in the definition of macros - this is one of the main reasons macros are considered bad and should be avoided except as a last resort. See this question for an example of a situation where someone completely correctly using the BitTest name for a template was screwed up because someone decided to create a macro using this name for the βaliasβ of another name: Win32: BitTest, BitTestAndComplement, .. . <- How to disable this garbage?
So, in your example, as the other answers were mentioned, after the preprocessing step has occurred, you will get something like the following fragment passed to the C compiler instead of defining your function:
int (int i>int j)? int i : int j{ return (i>j) ? i : j; }
This is not valid C, so you get a compiler error that might say (from GCC 3.4.5):
error: syntax error before "int"
or (from MSVC 9):
error C2059: syntax error : 'type'
Which really doesn't help much, because when you look at the line referenced by the error in your editor, it will look like this:
int MAX(int i, int j){
which looks valid.
There are several methods that help you avoid the problem:
use all caps for macro names and only macro names; itβs an agreement that is mostly followed to keep the macro namespace separate from the names used for other things.
puts parsers around names that you donβt want to expand as macros (this only works for function type macros). If your example was written as follows:
int (MAX)(int i, int j){ return (i>j) ? i : j; }
this would not expand the macro. However, I do not see people doing this very often. One set of utility routines that I use uses this technique to protect against macro name conflicts, and I often ask questions about why all function names are in parens. Also, several editors are confused with the navigation function for these files.
avoid using macros; as I said, this is one of the reasons that macros are considered bad. There are other reasons, including that they can easily lead to the fact that the extended form will create bad or unexpected expressions if you use parsers incorrectly around macro parameters or evaluate a macro parameter that has side effects more than once.
Instead, use built-in functions or function templates.
source share