Why is indirect

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 the specific preprocessor character.

If I just use CAT directly instead of CMB , SLB_ALGORITHM does not expand. Why is this and how exactly does indirect assistance help?

+5
source share
2 answers

To quote this answer :

When you have a macro replacement, the preprocessor will recursively expand the macros if neither the gating operator nor the marker binding operator ## apply to it.

Thus, the preprocessor does not extend this macro when ## is applied to it. This is why it is exapnded at the CMB(A, B) level, but not with direct use of CAT(X, Y) .

+2
source

## is a string concatenator, so if you call the CAT(Logging, SLB_ALGORITHM) macro CAT(Logging, SLB_ALGORITHM) from SLB_LOGGING_ALGORITHM , this will combine the Logging string with the SLB_ALGORITHM string, SLB_ALGORITHM .: LoggingSLB_ALGORITHM , which most likely will not be what you want.

When calling the CMB(Logging, SLB_ALGORITHM) macro CMB(Logging, SLB_ALGORITHM) from SLB_LOGGING_ALGORITHM instead, the preprocessor first expands Logging and SLB_ALGORITHM (calls CMB() ), then concatenates the expanded lines (calling CAT() ).

+5
source

All Articles