[preprocessor] using ENUM_PARAMS to generate macro call arguments
Hello, I'll try to illustrate what I am trying to do on this example: #define VER 1 // given alternative sets of macros: #if VER==1 #define M0() #define M1( arg0 ) #define M2( arg0, arg1 ) #define M3( arg0, arg1, arg2 ) #define M4( arg0, arg1, arg2, arg3 ) #elif VER==2 #define V0( x ) #define V1( x, arg0 ) #define V2( x, arg0, arg1 ) #define V3( x, arg0, arg1, arg2 ) #define V4( x, arg0, arg1, arg2, arg3 ) #endif // I'm trying to write alternative versions of INVOKE for the above sets of macros, // so that the interface below needs no #ifs #if VER==1 #define INVOKE( n_args ) \ BOOST_PP_CAT(M,n_args)( BOOST_PP_ENUM_PARAMS(n_args,arg) ) #elif VER==2 #define INVOKE( n_args ) \ BOOST_PP_CAT(V,n_args)( dummy BOOST_PP_ENUM_TRAILING_PARAMS(n_args,arg) ) #endif // the interface implemented in terms of INVOKE #define A0() INVOKE(0) #define A1( arg0 ) INVOKE(1) #define A2( arg0, arg1 ) INVOKE(2) #define A3( arg0, arg1, arg2 ) INVOKE(3) #define A4( arg0, arg1, arg2, arg3 ) INVOKE(4) int main() { A2(1,2) } My goal here is to keep INVOKE as simple and non-redundant as possible, because alternative versions of INVOKE for alternative sets of given macros will be added later on. Now the call A2(1,2) doesn't compile. If I understand correctly, the reason is ENUM_PARAMS(n_args,arg) is passed to M2, while I need the result of macro expansion of ENUM_PARAMS(...) to be passed to M2. So my question is: Is there a way around this? I attached a file ready to compile with the presented code. Thanks in advance Kris
On 15 June 2011 14:15, Krzysztof Czainski <1czajnik@gmail.com> wrote:
My goal here is to keep INVOKE as simple and non-redundant as possible, because alternative versions of INVOKE for alternative sets of given macros will be added later on. Now the call A2(1,2) doesn't compile. If I understand correctly, the reason is ENUM_PARAMS(n_args,arg) is passed to M2, while I need the result of macro expansion of ENUM_PARAMS(...) to be passed to M2. So my question is: Is there a way around this?
Maybe BOOST_PP_EXPAND? http://www.boost.org/libs/preprocessor/doc/ref/expand.html
2011/6/16 Daniel James
On 15 June 2011 14:15, Krzysztof Czainski <1czajnik@gmail.com> wrote:
My goal here is to keep INVOKE as simple and non-redundant as possible, because alternative versions of INVOKE for alternative sets of given macros will be added later on. Now the call A2(1,2) doesn't compile. If I understand correctly, the reason is ENUM_PARAMS(n_args,arg) is passed to M2, while I need the result of macro expansion of ENUM_PARAMS(...) to be passed to M2. So my question is: Is there a way around this?
Maybe BOOST_PP_EXPAND?
http://www.boost.org/libs/preprocessor/doc/ref/expand.html
Exactly what I needed, thanks a lot, Daniel ;-)
I attach the complete solution using EXPAND in case anyone is interested. Cheers, Kris
participants (2)
-
Daniel James
-
Krzysztof Czainski