_cplusplus is a standard macro defined by all C++ compilers (AFAIK, anyway). It's used when the code is expected to be compiled by both C and C++ compilers. For example, in headers that need the extern "C" statement, which is C++ only.
_WIN32 and all of its variations are defined by compilers that produce code for Windows. Likewise, there's a unix macro defined when the compiler produces code for some POSIX system.
There are quite a few macros like these, including compiler, compiler version, language standard version, libraries, and architectures:
http://predef.sourceforge.net/index.php
Here's one of my favorite macros:
#define ABS(x) ((x)<0?-(x):(x))
It's four or five times faster than abs(), even for integers (possibly due to the implicit conversion to and from floating point).
I've also used macros when I needed to do the same thing many times, but where functions weren't quite as useful. The example in particular is "parse an element from a vector and put the result into something; if there are any errors return an error code". While that
can be done with a function, this:
1 2 3
|
ErrorCode error=parse(vector[n],something);
if (error!=NO_ERROR)
return error;
|
is not as practical as
1 2 3 4 5 6 7
|
#define _PARSE(src,dst) {\
ErrorCode error=parse(vector[(src)],(dst));\
if (error!=NO_ERROR)\
return error;\
}
//...
_PARSE(n,something)
|
Keep in mind that the assignment and check has to be done each time an element is parsed in any function.
Here's a more literal example. Notice how _GETWCSVALUE and _GETINTVALUE are all over the place:
http://onslaught-vn.svn.sourceforge.net/viewvc/onslaught-vn/trunk/src/Processing/ScriptInterpreter_commandsAI.cpp
I don't think there are many more uses for macros other than that, forced inlined functions, and creating aliases for identifiers.