prix prad wrote:
I encountered the above error when I tried to expand a macro
as follows:
#define EXPAND(array) int M_ ## array
The problem occurs when we have 'array' itself as a macro:
say EXPAND(ARR(DECL))
where #define DECL _decl
#define ARR(name) arr ## name
So the end result would be M_arr_name
The error goes away when I do:
#define EXPAND(array) int PREPEND_ARR(array)
where
PREPEND_ARR(array) M_ ## array
Can someone explain me why the error is about a 'function declaration'
in this case since we are dealing with MACROS?
...
C preprocessor does not perform preliminary macro expansion for arguments
adjacent to ## operator. For this reason in your first variant of EXPAND the
argument ARR(DECL) is not expanded and
EXPAND(ARR(DECL))
first turns into
int M_ARR(DECL)
and later, after rescanning into the end result
int M_ARR(_decl)
Since 'ARR' get concatenated with 'M_', the preprocessor never gets a chance to
identify that 'ARR' with your 'ARR' macro.
This final result looks like a function declaration to the compiler, which is
why it complains about function declaration.
In your second variant of EXPAND theres no ## in EXPAND and for this reason in
EXPAND(ARR(DECL))
the preliminary macro expansion is applied to the argument ARR(DECL), resulting in
int PREPEND_ARR(arr_decl)
the further rescan turns this into
int M_arr_decl
--
Best regards,
Andrey Tarasevich