0

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.

10
  • 1
    which line is eq.h(11)? did you remember to put your code that does things inside a function? Commented Jul 19, 2021 at 10:40
  • 3
    Please try to create a proper minimal reproducible example to show us. Commented Jul 19, 2021 at 10:41
  • On another couple of notes, you have some other issues in the code you show... Like a one-off error in the loop which will cause it to go out of bounds; Using malloc instead of new[] (or better yet, std::vector or std::array); And using uninitialized values. Commented Jul 19, 2021 at 10:42
  • i <= size is suspicious, Do you mean i < size? Commented Jul 19, 2021 at 10:42
  • 2
    Why not use std::vector, and std::transform? Commented Jul 19, 2021 at 10:44

3 Answers 3

2

Short answer, don't do that. Since you've templates you can use standard library approach from functional header

without any macro definitions at all.

For example:

template<typename E, class Pred>                                                   
void vcompare(E *compArrayA, E *compArrayB, bool *resArray, int size) {
    for (int idx = 0; idx <= size; ++idx ) {                            
        resArray[idx] = Pred(compArrayA[idx], compArrayB[idx]);
    }                                                               
}

vcompare<int,std::less>(lhs,rhs,res,len);
vcompare<int,std::greater>(lhs,rhs,res,len);

P.S. Check std::vector class and general STL part inside standard C++ library.

Sign up to request clarification or add additional context in comments.

1 Comment

Using std::vector and std::transform (as suggested in one comment) would make this even easier: std::vector<float> v1(100), v2(100); std::vector<bool> res(100); std::transform(begin(v1), end(v1), begin(v2), begin(res), std::less<float>());
1

As you define your MACRO, you shouldn't use ; after it, so

COMPARE(==, eq)
COMPARE(!=, nq)
COMPARE(>=, greater_eq)

instead of

COMPARE(==, eq);
COMPARE(!=, nq);
COMPARE(>=, greater_eq);

2 Comments

Thanks for answering. I removed the \ in the last line of the macro and removed the ; in the macro declaration, but I still get the same error
@BoKuToTsuZenU • The code you've posted here is not the code that is producing the error you are experiencing. Please provide a minimal reproducible example. Help us help you.
0

What you are trying to do is to pass operator as an argument.

Passing an operator as an argument to a function in C

As Victor Gubin already answered it is best to avoid macro and embrace modern alternatives to do the same you are trying to achieve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.