Re: Macro redefinition



Ben Pfaff wrote:
Tor Rustad <tor_rustad@xxxxxxxxxxx> writes:

void gen_source_function(FILE *out)
{
#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
#include "iso8583_defs"

#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
#include "iso8583_defs"

}

where the "iso8583_defs" file, did contain a lot of lines like this:

EXPAND_DEF(1, FIELD_NAME);
EXPAND_DEF(2, ANOTHER_FIELD_NAME);


My question is simply, is the above C code allowed?

Sure. Lots of code uses this trick to avoid redundancy.

Yup, that's the idea.

If you don't like the idea of using an include file for this, you
can also use a macro:

void gen_source_function(FILE *out)
{
#define ISO8583_DEFS \
EXPAND_DEF(1, FIELD_NAME); \
EXPAND_DEF(2, ANOTHER_FIELD_NAME);

#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
ISO8583_DEFS

#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
ISO8583_DEFS
}

Interesting, I haven't seen this solution before.

However, there is a translation limit of "509 characters in a logical source line", and isn't logical source lines made up after line-splicing your ISO8583_DEFS above?

--
Tor <torust [at] online [dot] no>
.