6

English is not my native language; thus I show the code to depict.

#define concat_temp(x, y) x##y
#define concat(x, y) concat_temp(x, y)
#define BAR 2

int main() {
    concat_temp(FOO, BAR)
    concat(FOO, BAR)
}

When I executed clang -E, the macros were expanded to

int main() {
    FOOBAR
    FOO2
}

Who can explain to me why concat_temp couldn't expand the bar to 2 but concat did the trick?

unlsycn
  • 59
  • 4

1 Answers1

5

According to CppReference,

A ## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers (which are not macro-expanded first) and then concatenates the result.

The ## operator operates on the identifiers themselves, so another pass of macro expansion is required if you want to "make string literal" or concatenate the values of #define-d macros.

iBug
  • 35,554
  • 7
  • 89
  • 134