I have defined the macro as follows
#define COMPARE(OPERATOR, FUNCTION) \
template <typename T> \
void FUNCTION(T *compArrayA, T *compArrayB, bool *resArray, int size) { \
for (int idx = 0; idx <= size; ++idx ) { \
resArray[idx] = (compArrayA[idx] OPERATOR compArrayB[idx]); \
} \
} \
COMPARE(==, eq);
COMPARE(!=, nq);
COMPARE(>=, greater_eq);
So, I tried to call the function defined in the macro as follows.
float *ArrayA, *ArrayB;
bool *ArrayRes;
ArrayA = (float *) malloc(sizeof(float)* 100);
ArrayB = (float *) malloc(sizeof(float)* 100);
ArrayRes = (bool *) malloc(sizeof(bool) * 100);
eq(ArrayA, ArrayB, ArrayRes, 100);
I received the following error.
eq.h(11): error: expected an expression
1 error detected in the compilation of "eq.cpp".
eq.h(11) represents the following line.
COMPARE(==, eq);
How do I define a function in a macro?
I just want to know how to implement functions using macros.
eq.h(11)? did you remember to put your code that does things inside a function?mallocinstead ofnew[](or better yet,std::vectororstd::array); And using uninitialized values.i <= sizeis suspicious, Do you meani < size?std::vector, andstd::transform?