mardi 5 novembre 2019

Conditional compilation based on string passed in macro

I have a macro of the form:

#define SAMPLE_MACRO(x) somefunction(x)

I invoke the macro with strings as parameter:

SAMPLE_MACRO("abc");
SAMPLE_MACRO("bbe");
SAMPLE_MACRO("ccb");
SAMPLE_MACRO("axx");

Is it possible to make the preprocessor parse the string so that if lets say it starts with an "a" (so strings "abc" and "axx" in the above example) the macro will call some other function and not somefunction()? In other words I do not want to add an "if" statement inside the macro that is executed at runtime and compares the string, but I want to have the macro have different behavior depending on the string parameter specified at compilation. I presume this may involve adding some #ifdef inside the macro. Is this possible in C++11?

Aucun commentaire:

Enregistrer un commentaire