LCID Fire wrote:
Is there a way to prevent macro prescanning?
I have the following problem:
#define APE 4
#define GET_ANIMAL_NAME(a, b) ANIMAL_##b##_##a
#define CREATE_ANIMAL_ENTRY(a, b) GET_ANIMAL_NAME(a, b) = (a + b)
enum animals
{
CREATE_ANIMAL_ENTRY(2, APE)
};
this expands to
ANIMAL_4_2 = (2 + 4)
but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME)
to expand this to:
ANIMAL_APE_2 = (2 + 4)
I know it works when I move the content of GET_ANIMAL_NAME to
CREATE_ANIMAL_ENTRY - but I need the name macro in other files, too.
I failed to note you last sentence. One way to do this is with compound
macros in which a single macro contains multiple fields. Here you want to
be able to select either the name, APE, or an associated value 4. Here is
one, admittedly ugly, way to do this:
#define APE (APE,4) /* compound definition */
/* ugly macros */
#define ARG2_1(a,b) a
#define ARG2_2(a,b) b
#define ANIMAL_NAME(a) ARG2_1 a
#define ANIMAL_NUM(a) ARG2_2 a
#define GET_ANIMAL_NAME3(a, b) ANIMAL_## b ##_## a
#define GET_ANIMAL_NAME2(a, b) GET_ANIMAL_NAME3(a,b)
#define GET_ANIMAL_NAME(a, b) GET_ANIMAL_NAME2(a, ANIMAL_NAME(b))
#define CREATE_ANIMAL_ENTRY(a, b) GET_ANIMAL_NAME(a, b) = \
(a + ANIMAL_NUM(b))
enum animals
{
CREATE_ANIMAL_ENTRY(2, APE),
GET_ANIMAL_NAME(2,APE),
ANIMAL_NAME(APE),
ANIMAL_NUM(APE)
};
This expands to (with my compiler)
13: enum animals
14: {
15: ANIMAL_APE_2 = (2 + 4),
16: ANIMAL_APE_2,
17: APE,
18: 4
19: };
I have used this technique to assign several values with a single name and
use macros to extract the desired component.
--
Thad