#define with two markers after the pattern

Each google link shows only simple examples, I have this code for the code:

#define XHANDLER(A,B,H) X_TO_BUS_HANDLER(A,B,H) X_FROM_BUS_HANDLER(A,B,H)

 namespace{ X_TO_BUS_HANDLER( some::SomeClassX, bus::SomeBus, foo::SomeHandler ); 

Does anyone know how this definition works? One template and two token lists? Links please.

I used the code, but only found X_TO_BUS_HANDLER.

+4
source share
3 answers

The C / C ++ preprocessor will replace the template for everything that is written on the same line. In your case, it looks as if the two tokens after this template themselves are macros, so they will also be expanded.

Example:

 #define F(x, y) xf(y yParam); #define G(x, y) yg(x xParam); #define FG(x, y) F(x, y) G(x, y); FG(int, double) //this is the same as: int f(double yParam); double g(int xParam); 

In your case, I assume that the two define X_FROM _... and X_TO _... create some functions or classes that are handlers for transferring X from or to some bus, respectively. The XHANDLER macro will create handlers for both directions.

+2
source

It works like any other definition - whenever a preprocessor collides with XHANDLER , it replaces it with X_TO_BUS_HANDLER(A,B,H) X_FROM_BUS_HANDLER(A,B,H) (and parameters).

The macro is not used in your fragment.

But something like

 XHANDLER(some::SomeClassX, bus::SomeBus, foo::SomeHandler) 

will be equivalent

 X_TO_BUS_HANDLER(some::SomeClassX, bus::SomeBus, foo::SomeHandler) X_FROM_BUS_HANDLER(some::SomeClassX, bus::SomeBus, foo::SomeHandler) 
+6
source

Remember that the preprocessor simply replaces the macros with your body. Therefore using a macro

 XHANDLER(a, b, c) 

just replaced by text

 X_TO_BUS_HANDLER(a, b, c) X_FROM_BUS_HANDLER(a, b, c) 
+5
source

Source: https://habr.com/ru/post/1447750/


All Articles