C Macros: Function factories, why the macro don't works only in one case?
i've written this two macros:
// Magic Assert Equal Atomic constructor generator
#define _GENERIC_ASSERT_EQ_ATOMIC_CONSTRUCTOR_(n, N, W, tt) \
assert_data_t *assert_eq_##n##_constructor (tt a, tt b, int passed) { \
return assert_data_constructor (_ASSERT_EQ_##N##_, passed, W(a), W(b)); \
}
// Magic Assert Equal Vector constructor generator
#define _GENERIC_ASSERT_EQ_VECTOR_CONSTRUCTOR_(n, N, W, tt) \
assert_data_t *assert_eq开发者_如何学运维_##n##_vector_constructor \
(tt * a, tt * b, int n, int passed) { \
return assert_data_constructor \
(_ASSERT_EQ_##N##_VECTOR_, passed, W##Vector(a, n), W##Vector(b, n)); \
}
the first macro works well (in the cases i've tried):
_GENERIC_ASSERT_EQ_ATOMIC_CONSTRUCTOR_(int, INT, Int, int)
_GENERIC_ASSERT_EQ_ATOMIC_CONSTRUCTOR_(flt, FLT, Flt, float)
_GENERIC_ASSERT_EQ_ATOMIC_CONSTRUCTOR_(dbl, DBL, Dbl, double)
_GENERIC_ASSERT_EQ_ATOMIC_CONSTRUCTOR_(complex_flt, COMPLEX_FLT, ComplexFlt, complex float)
_GENERIC_ASSERT_EQ_ATOMIC_CONSTRUCTOR_(complex_dbl, COMPLEX_DBL, ComplexDbl, complex double)
_GENERIC_ASSERT_EQ_ATOMIC_CONSTRUCTOR_(str, STR, Str, char *)
but the second macro... doesn't work well in the 'int' case (with float and double yes):
_GENERIC_ASSERT_EQ_VECTOR_CONSTRUCTOR_(int, INT, Int, int) // Here i have an error
_GENERIC_ASSERT_EQ_VECTOR_CONSTRUCTOR_(flt, FLT, Flt, float)
_GENERIC_ASSERT_EQ_VECTOR_CONSTRUCTOR_(dbl, DBL, Dbl, double)
the error that gcc shows me is:
unitarium4c.c:115: error: two or more data types in declaration specifiers unitarium4c.c: In function ‘assert_eq_int_vector_constructor’: unitarium4c.c:115: error: parameter name omitted unitarium4c.c:115: error: expected expression before ‘int’ unitarium4c.c:115: error: expected expression before ‘int’
:p i don't understand why appears that error. (If I copy the macro and expand it, it works well in the 'int' case).
Thanks in advance :) .
Try
// _GENERIC_ASSERT_EQ_VECTOR_CONSTRUCTOR_(int, INT, Int, int) // Here i have an error
_GENERIC_ASSERT_EQ_VECTOR_CONSTRUCTOR_(integer, INT, Int, int)
// _____^^^^^^^_____
The expansion of the original macro would create
assert_data_t *assert_eq_int_vector_constructor \
(int * a, int * b, int int, int passed) { \
/* __________________^^^^^^^__ */
return assert_data_constructor \
(_ASSERT_EQ_INT_VECTOR_, passed, IntVector(a, n), IntVector(b, n)); \
}
You can also tell gcc to stop "compiling" after expanding the macros and check the resulting code. Try
gcc -E source
You have a macro parameter n
, and a function parameter int n
in the macro body, so that's becoming int int
.
精彩评论