define: is there a way to expand arguments?

Viewed 212

T() is a macro

I have many things like:

MACRODOWN(T(P), T(E), T(X), T(R), T(T), T(H), T(A), T(F))
MACRODOWN(T(H), T(A), T(F))
MACRODOWN(T(Z), T(A))

I'd like to define a macro that would work like this:

MACRODOWN(TT(P,E,X,R,T,H,A,F))
MACRODOWN(TT(H,A,F))
MACRODOWN(TT(Z,A))

And they would exand, of course, to the first macros above. Is this possible, and if so, how?

2 Answers

The answer is: No, The GCC can't do argument expansion. The C Preprocessor only supports 2 kind of macros: Object-like macros and function-like macros. What you are using is function-like macro. A Macro is code which has been given a name. A function-like macro expands its code trough the compiler, but take care about this, only its code, not its arguments, and if contains other macros the compiler will expand too the other macros code. The Official page of the GCC (https://gcc.gnu.org/), NEVER reports that it supports argument expansion, or what I see what you want, function argument separation at expansion time. The GCC supports only the normal macro code expansion (here is the specific article about supported macros: https://gcc.gnu.org/onlinedocs/cpp/Macros.html), so there is no way to do function argument separation at expansion time. But you can perfectly define the macros like the second example you give, but of course will not do argument expansion.

a boost preprocessor solution could be

#include <boost/preprocessor/variadic/to_seq.hpp>
#include <boost/preprocessor/seq/enum.hpp>
#include <boost/preprocessor/seq/transform.hpp>


#define TT(...) BOOST_PP_SEQ_ENUM( BOOST_PP_SEQ_TRANSFORM( TT_, _, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__) ) )
#define TT_(_1,_2,E) T(E)

TT(A,B,C,D) // T(A),T(B),T(C),T(D)

where is assumed variadic support and boost pp iteration limits.

Related