As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ##
preprocessor operator. According to K&R:
The preprocessor operator
##
provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a##
, the parameter is replaced by the actual argument, the##
and surrounding white space are removed, and the result is re-scanned. For example, the macropaste
concatenates its two arguments:
#define paste(front, back) front ## back
so
paste(name, 1)
creates the tokenname1
.
How and why would someone use this in the real world? What are practical examples of its use, and are there gotchas to consider?
You can use token pasting when you need to concatenate macro parameters with something else.
It can be used for templates:
In this case LINKED_LIST(int) would give you
Similarly you can write a function template for list traversal.
It is very useful for logging. You can do:
Or, if your compiler doesn't support function and func:
The above "functions" logs message and shows exactly which function logged a message.
My C++ syntax might be not quite correct.
SGlib uses ## to basically fudge templates in C. Because there's no function overloading, ## is used to glue the type name into the names of the generated functions. If I had a list type called list_t, then I would get functions named like sglib_list_t_concat, and so on.
A previous question on Stack Overflow asked for a smooth method of generating string representations for enumeration constants without a lot of error-prone retyping.
Link
My answer to that question showed how applying little preprocessor magic lets you define your enumeration like this (for example) ...;
... With the benefit that the macro expansion not only defines the enumeration (in a .h file), it also defines a matching array of strings (in a .c file);
The name of the string table comes from pasting the macro parameter (i.e. Color) to StringTable using the ## operator. Applications (tricks?) like this are where the # and ## operators are invaluable.
One important use in WinCE:
While defining register bit description we do following:
And while using BITFMASK, simply use:
CrashRpt: Using ## to convert macro multi-byte strings to Unicode
An interesting usage in CrashRpt (crash reporting library) is the following:
Here they want to use a two-byte string instead of a one-byte-per-char string. This probably looks like it is really pointless, but they do it for a good reason.
They use it with another macro that returns a string with the date and time.
Putting
L
next to a__ DATE __
would give you a compiling error.Windows: Using ## for generic Unicode or multi-byte strings
Windows uses something like the following:
And
_T
is used everywhere in codeVarious libraries, using for clean accessor and modifier names:
I've also seen it used in code to define accessors and modifiers:
Likewise you can use this same method for any other types of clever name creation.
Various libraries, using it to make several variable declarations at once: