I was wondering why we can't use token concatenation outside of define
s.
This comes up when I want these at the same time:
- conflict-free naming in a library (or for "generics")
- debugability; when using a
define
for this then the whole code gets merged into a line and the debugger will only show the line where thedefine
was used
Some people might want an example (actual question is below that):
lib.inc:
#ifndef NAME
#error includer should first define NAME
#endif
void NAME() { // works
}
// void NAME##Init() { // doesn't work
// }
main.c:
#define NAME conflictfree
#include "lib.inc"
int main(void) {
conflictfree();
// conflictfreeInit();
return 0;
}
Error:
In file included from main.c:2:0:
lib.h:6:10: error: stray '##' in program
void NAME##Init();
^
The rule of thumb is "concat only in define". And if I remember correctly: The reason is because of the preprocessor-phases.
Question: Why does it not work. The phases-argument sounds like it was once an implementation-limitation (instead of a logical reason) and then found its way into the standard. What could be so difficult about accepting NAME##Init()
if NAME()
works fine?
#
.. I guess the preprocessor devs just lazy to parse the other stuff.NAME##Init
versus#define NAME_(x) NAME##x
plusNAME_(Init)
probably is not worth it.##
.