It is necessary to understand how macro replacement works in C, particularly in the context of concatenating tokens using the ## operator and converting macro parameters to strings using the # operator.
| Wiki Markup |
|---|
It is often useful to merge two tokens into one while expanding macros. This is called token pasting or token concatenation. The {{##}} preprocessing operator performs token pasting. When a macro is expanded, the two tokens on either side of each ## operator are combined into a single token, which replaces the {{##}} and the two original tokens in the macro expansion \[[FSF 05|AA. C References#FSF 05]\]. |
Token pasting is most useful when one or both of the tokens comes from a macro argument. If either of the tokens next to an ## is a parameter name, it is replaced by its actual argument before ## executes. The actual argument is not macro-expanded first.
Non-Compliant Code Example
| Wiki Markup |
|---|
The following definition for {{static_assert()}} from \[[DCL03-A. Use a static assertion to test the value of a constant expression]] uses the {{JOIN()}} macro to concatenate the token {{assertion_failed_at_line_}} with the value of {{\_\_LINE\_\_}}. |
| Code Block |
|---|
#define JOIN(x, y) x ## y
#define static_assert(e, s) \
typedef char JOIN(assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
|
| Wiki Markup |
|---|
{{\_\_LINE\_\_}} is a predefined macro names which expands to an integer constant representing the presumed line number of the current source line within the current source file \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]. |
If the intention is to expand the __LINE__ macro, which is likely the case here, the following definition for JOIN() is non-compliant:
| Code Block |
|---|
|
#define JOIN(x, y) x ## y
|
because However, the __LINE__ is not expanded, and the character array is subsequently named assertion_failed_at_line___LINE__.
Compliant Solution
To get the macro to expand, a second level of indirection has to be added as in the following examplethis compliant solution:
| Code Block |
|---|
|
#define JOIN(x, y) JOIN_AGAIN(x, y)
#define JOIN_AGAIN(x, y) x ## y
|
...
| Wiki Markup |
|---|
Note also that macro parameters cannot be individually parenthesized when concatenating tokens using the {{##}} operator, converting macro parameters to strings using the {{#}} operator, or concatenating adjacent string literals. This is an exceptionexception *PRE01-EX2* to \[[PRE01-A. Use parentheses within macros around parameter names]\]. |
...