You can solve the case of MACRO_REPEAT(ARRAY_LENGTH , -2) by changing the definition of MACRO_REPEAT to use a two-stage extension, i.e. do not use token insertion in MACRO_REPEAT , call another macro that does.
Not that it will only work as expected if ARRAY_LENGTH is defined as a token with a single number, and if there is a macro definition for this particular size.
You cannot handle the more general MACRO_REPEAT(X*Y , -2) case with the standard C preprocessor.
You can use the gcc extension to initialize simple arrays:
#define MACRO_REPEAT(n, e) [ 0 ... (n)-1 ] = (e),
But this method cannot be used to process multidimensional arrays such as MACRO_REPEAT(X*Y , -2) .
You can try the following:
#define MACRO_REPEAT(n, e) [ 0 ... (n)-1 ] = (e), #define X 512 #define Y 512 const int array[X][Y] = { MACRO_REPEAT(X, { MACRO_REPEAT(Y, -2) }) };
But using the C preprocessor simply hides the intention. If you decide to rely on gcc extensions, just use them directly.
source share