Re: Macro question
In article <1180542675.843454.130660@q75g2000hsh.googlegroups.com>,
Peithon <Peithon@googlemail.com> wrote:
Hi,
I'm initialising unit test structures using a macro as follows:
#define UTMACRO(x) UT##x
typedef struct
{
char * key;
char * arr[5];
}unit_test;
unit_test UT1 = { "three", { "five", "four", "one", "three",
"two" } };
unit_test UT2 = { "two", { "five", "four", "one", "three",
"two" } };
int main()
{
int i;
unit_test unitTest[2];
for(i = 0; i < TEST_NUM; i++)
{
unitTest[i] = UTMACRO(i + 1);
}
}
but the compiler is resolving UTMACRO(i + 1) as UT rather than UT1 and
then UT2.
Why? Can this be fixed or does anyone have a better way of performing
this task.
it appears that you want main to contain an array of N unit_tests
initialized as {UT1,UT2,...UTN} where N is know at preprocessor time.
If so boost's preprocessor library is probably should be a friend of
yours, as it contains a macro BOOST_PP_ENUM_PARAMS , that will do
just that. Namely
int main()
{
unit_test unitTest[]= {BOOST_PP_ENUM_PARAMS(UT,TEST_NUM)};
// ...
}
Why it fails is + means litte to the preprocesor and i's value is not
know until runtime. The expansion of UTMACRO(i+1) btw is UTi+1
not UT. Direct text substition.
--
W
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]