Is it possible to have one macro expanded differently for one specific argument value and differently for all other arguments?
Say I define a current user:
#define CURRENT_USER john_smith
What I want to be able to do is to have a macro that will be expanded differently if the user passed matches CURRENT_USER
. Mind you, I don't know all possible users a priori. The most basic case:
#define IS_CURRENT_USER(user) \
/* this is not valid preprocessor macro */ \
#if user == CURRENT_USER \
1 \
#else \
0 \
#endif
With a macro like that, every other macro relying on the username could be done the following way:
#define SOME_USER_SPECIFIC_MACRO(user) SOME_USER_SPECIFIC_MACRO_SWITCH_1(IS_CURRENT_USER(user))
#define SOME_USER_SPECIFIC_MACRO_SWITCH_1(switch) SOME_USER_SPECIFIC_MACRO_SWITCH_2(switch) // expand switch ...
#define SOME_USER_SPECIFIC_MACRO_SWITCH_2(switch) SOME_USER_SPECIFIC_MACRO_##switch // ... and select specific case
#define SOME_USER_SPECIFIC_MACRO_0 ... // not current user
#define SOME_USER_SPECIFIC_MACRO_1 ... // current user
Is this possible?
EDIT: Let me clarify. Say each programmer defines a different CURRENT_USER
in their config header. I want user-specific macros to expand to something meaningful if and only if their user
argument matches CURRENT_USER
. As I would like those macros to contain _pragma
s, it can't be runtime check (as proposed in some answers below).
EDIT: Again, clarification. Say there's a macro to disable the optimisation of some sections of code:
#define TURN_OPTIMISATION_OFF __pragma optimize("", off)
Some programmers want to turn optimisation off for different sections of code but not all at one time. What I'd like is to have a macro:
#define TURN_OPTIMISATION_OFF(user) /* magic */
That will match the user
argument against the CURRENT_USER
macro, taken from the per-programmer config file. If the user matches, the macro is expanded into pragma. If not, to nothing.
user
argument come from? Is a preprocessed compler constant?? Why does the_Pragma
requirement precludes "runtime" checking (which a good optimizing compiler would optimize at compile-time!). You really should tell more, showing more code, and explaining more. You also should look at the preprocessed output to understand the role of the preprocessor.Makefile
-s) so that the compiler is invoked with e.g.-DCURRENT_USER_ID=$(shell id -u)
, assuming a Linux system with GNUmake
)?CURRENT_USER
is irrevelant in this case.