[preprocessor] ifdef equivalent inside a macro, is it possible?

Hi all, Would there be a way to do some "ifdef" inside a macro? Could be in the form of a macro taking 3 args, - 1st being the macro to test the existance of, - 2nd being the value to expand in case 1st arg is a defined macro - 3rd being the value to expand in case 1st arg is not a defined macro Exemple: #define DUMMY_MACRO1 foo1 // next is commented out and not defined // #define DUMMY_MACRO2 foo2 PP_IFDEF(DUMMY_MACRO1, DUMMY_MACRO1, bar1) // would expand to foo1 PP_IFDEF(DUMMY_MACRO2, DUMMY_MACRO2, bar2) // would expand to bar2 Seems impossible to me, but boost/preprocessor does so many things that seemed impossible to me. Maybe someone will have some more magic... Thanks. Damien.

On 10/08/2011 15:15, damien benoist wrote:
Hi all, Would there be a way to do some "ifdef" inside a macro? Could be in the form of a macro taking 3 args, - 1st being the macro to test the existance of, - 2nd being the value to expand in case 1st arg is a defined macro - 3rd being the value to expand in case 1st arg is not a defined macro
Not really. You can test whether a macro expands to 0 or 1 though.

Would there be a way to do some "ifdef" inside a macro?
Could be in the form of a macro taking 3 args, - 1st being the macro to test the existance of, - 2nd being the value to expand in case 1st arg is a defined macro - 3rd being the value to expand in case 1st arg is not a defined macro
Not really. You can test whether a macro expands to 0 or 1 though.
Yes, I've seen that. I think I've also seen the possibility to test wether it expends to 0 or any number from 1 to some maximum. And some people here also talked about chaos. It seems that it also alowsto test wether a macro expands to "0 and nothing (i.e. a placemarker)" (chaos_pp_placemarker_if) It is described as C99 specific (I suppose it uses __VA_ARGS__). But what I would like is to expand to some value by default and another if a given macro is set. Thanks for your answer.

On 08/10/2011 05:22 PM, damien benoist wrote:
Yes, I've seen that.
I think I've also seen the possibility to test wether it expends to 0 or any number from 1 to some maximum.
And some people here also talked about chaos. It seems that it also alowsto test wether a macro expands to "0 and nothing (i.e. a placemarker)" (chaos_pp_placemarker_if) It is described as C99 specific (I suppose it uses __VA_ARGS__).
But what I would like is to expand to some value by default and another if a given macro is set.
You can do something like: #ifdef FOO # define FOO_ISSET 1 #else # define FOO_ISSET 0 #endif # define BAR BOOST_PP_IF(FOO_ISSET, FOO, bar) -- Maxime

You can do something like:
#ifdef FOO # define FOO_ISSET 1 #else # define FOO_ISSET 0 #endif
# define BAR BOOST_PP_IF(FOO_ISSET, FOO, bar)
Yes, that's exactly my problem. I have many cases like this. and I would like to not have to write 5 lines for each. So it is rather something like this: #define FOO1 foo1 #define FOO2 foo2 ... #define FOO42 foo42 #define FOO24_OVERRIDE myFoo24 #ifdef FOO1_OVERRIDE # define FOO1_OVERRIDE_ISSET 1 #else # define FOO1_OVERRIDE_ISSET 0 #endif ... #ifdef FOO42_OVERRIDE # define FOO42_OVERRIDE_ISSET 1 #else # define FOO42_OVERRIDE_ISSET 0 #endif In my case it would be simpler to add: #define FOO1_OVERRIDE FOO1 ... #define FOO23_OVERRIDE FOO23 #define FOO25_OVERRIDE FOO25 ... #define FOO42_OVERRIDE FOO42 Then there would be no more need for the if. But I would like to not even have one line to write for each macro. Thanks for your answer.

On 08/10/2011 06:57 PM, damien benoist wrote:
Then there would be no more need for the if. But I would like to not even have one line to write for each macro.
Thanks for your answer.
Is it possible for you to make the macro defined to some special value rather than being undefined ? Something like : #include <boost/preprocessor/control/if.hpp> #include <boost/preprocessor/facilities/empty.hpp> #include <boost/preprocessor/list/adt.hpp> #ifdef DEFINE_FOO # define FOO (foo, BOOST_PP_NIL) #else // Rather than being undefined, make it an empty list # define FOO (BOOST_PP_NIL) #endif # define BAR BOOST_PP_IF(BOOST_PP_LIST_IS_NIL(FOO), \ bar,\ BOOST_PP_LIST_FIRST(FOO)) -- Maxime

Is it possible for you to make the macro defined to some special value
rather than being undefined ?
Yes, but I have many cases like this. This time it will look like this (the ifdef DEFINE_FOO is not needed): // my default values #define FOO1 foo1 #define FOO2 foo2 ... #define FOO42 foo42 // optional overriding values # define FOO1_OVERRIDE (BOOST_PP_NIL) ... # define FOO23_OVERRIDE (BOOST_PP_NIL) // The only macro I would like to have to write # define FOO24_OVERRIDE (myFoo24, BOOST_PP_NIL) # define FOO25_OVERRIDE (BOOST_PP_NIL) ... # define FOO42_OVERRIDE (BOOST_PP_NIL) And it turns out to be simpler to write: #define FOO1_OVERRIDE FOO1 ... #define FOO23_OVERRIDE FOO23 #define FOO24_OVERRIDE myFoo24 #define FOO25_OVERRIDE FOO25 ... #define FOO42_OVERRIDE FOO42 Thanks for your help.

On 11/08/2011 15:05, damien benoist wrote:
Is it possible for you to make the macro defined to some special value
rather than being undefined ?
Yes, but I have many cases like this. This time it will look like this (the ifdef DEFINE_FOO is not needed):
// my default values #define FOO1 foo1 #define FOO2 foo2 ... #define FOO42 foo42
// optional overriding values # define FOO1_OVERRIDE (BOOST_PP_NIL) ... # define FOO23_OVERRIDE (BOOST_PP_NIL)
// The only macro I would like to have to write # define FOO24_OVERRIDE (myFoo24, BOOST_PP_NIL)
# define FOO25_OVERRIDE (BOOST_PP_NIL) ... # define FOO42_OVERRIDE (BOOST_PP_NIL)
And it turns out to be simpler to write:
#define FOO1_OVERRIDE FOO1 ... #define FOO23_OVERRIDE FOO23 #define FOO24_OVERRIDE myFoo24 #define FOO25_OVERRIDE FOO25 ... #define FOO42_OVERRIDE FOO42
Thanks for your help.
If you can list the names of all possible macros in some place, then I think you can make some macro that tests if it is defined or not, assuming the definition is unary.

On 8/11/2011 10:25 AM, Mathias Gaunard wrote:
On 11/08/2011 15:05, damien benoist wrote:
Is it possible for you to make the macro defined to some special value
rather than being undefined ?
If you can list the names of all possible macros in some place, then I think you can make some macro that tests if it is defined or not, assuming the definition is unary.
I would like to see that code. Within a macro you can test a macro for a value, given a limited subset of possible values, but I do not think you can test whether a macro is actually defined or not without first setting another macro to a value based on it, as in: #if defined(SOME_MACRO) #define ANOTHER_MACRO 1 #else #define ANOTHER_MACRO 0 #endif #define TEST_MACRO BOOST_PP_IIF(ANOTHER_MACRO,DO_WHEN_SOME_MACRO_DEFINED,DO_WHEN_SOME_MACRO_UNDEFINED) Eddie

On 11/08/2011 21:26, Edward Diener wrote:
On 8/11/2011 10:25 AM, Mathias Gaunard wrote:
On 11/08/2011 15:05, damien benoist wrote:
Is it possible for you to make the macro defined to some special value
rather than being undefined ?
If you can list the names of all possible macros in some place, then I think you can make some macro that tests if it is defined or not, assuming the definition is unary.
I would like to see that code. Within a macro you can test a macro for a value, given a limited subset of possible values, but I do not think you can test whether a macro is actually defined or not
If it not defined, doesn't it just expand to the name of the macro, which could be part of the "limited subset of possible values"?

On 10/08/2011 17:22, damien benoist wrote:
Yes, I've seen that.
I think I've also seen the possibility to test wether it expends to 0 or any number from 1 to some maximum.
Actually, you can check whether it begins with any alphanumeric token of your choice.

I think I've also seen the possibility to test wether it expends to 0 or any number from 1 to some maximum.
Actually, you can check whether it begins with any alphanumeric token of your choice.
Hmmm, interesting... What macro are yout talking about? Do you have an example? Thanks!
participants (4)
-
damien benoist
-
Edward Diener
-
Mathias Gaunard
-
Maxime van Noppen