Consider the following macro:
#define CAT(X, Y) X ## Y
#define CMB(A, B) CAT(A, B)
#define SLB_LOGGING_ALGORITHM CMB(Logging, SLB_ALGORITHM)
where SLB_ALGORITHM
is a defined pre-processor symbol.
If I just use CAT
directly instead of CMB
, SLB_ALGORITHM
does not get expanded. Why is that the case and how exactly does the indirection help?
##
is a string concatenator, so if you call CAT(Logging, SLB_ALGORITHM)
from SLB_LOGGING_ALGORITHM
macro, this will result in concatenation of string Logging
with string SLB_ALGORITHM
, that is: LoggingSLB_ALGORITHM
which is likely not what you want.
When calling CMB(Logging, SLB_ALGORITHM)
from SLB_LOGGING_ALGORITHM
macro, instead, preprocessor first expands Logging
and SLB_ALGORITHM
(call to CMB()
) then concatenate the expanded strings (call to CAT()
).
To quote this answer:
When you have a macro replacement, the preprocessor will only expand the macros recursively if neither the stringizing operator # nor the token-pasting operator ## are applied to it.
So the preprocessor does not expand a given macro when ##
is applied to it. This is why it is exapnded in the CMB(A, B)
level but not when directly using CAT(X, Y)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With