1

In this snippet, I can't understand what the FUNCTION(func,pred) is supposed to be doing.

#define FUNCTION(func,pred)\
void func(int& a, int x){ \
if(!(a pred x)) a = x; \
}

I also can't understand this:

FUNCTION(minimum, <)
FUNCTION(maximum, >)

Can somebody explain please?

Brian Tompsett - 汤莱恩
5,92772 gold badges63 silver badges135 bronze badges
asked May 20, 2016 at 17:07
8
  • 5
    What part of the code confuses you? Commented May 20, 2016 at 17:10
  • 2
    It would be helpful if you went through and explain how you think it works and we can correct you if that is incorrect/there is missing understanding. Commented May 20, 2016 at 17:11
  • Hello Thomas, I am being confused about of "FUNCTION (func,pred)" of the preprocessor and two FUNCTIONs just above int main(). Commented May 20, 2016 at 17:11
  • Use the macro, and then run the file though the preprocessor and see that the macro invocation is replaced with. All good compilers have options to stop after running the preprocessor. Commented May 20, 2016 at 17:12
  • Hello Nathan, thanks for the feedback I should edit my questions :) Commented May 20, 2016 at 17:12

2 Answers 2

3

The preprocessor substitutes all tokens: FUNCTION(func,pred) with

void func(int& a, int x){
if(!(a pred x)) a = x;
}

Thus, defining a function func with a binary operator as pred. So, when you write this:

FUNCTION(minimum,<)

The preprocessor replaces that with:

void minimum(int& a, int x){
 if(!(a < x))
 a = x;
}

Same thing happens with:

FUNCTION(maximum,>)

produces:

void maximum(int& a, int x){
 if(!(a > x))
 a = x;
}

...and so on..

answered May 20, 2016 at 17:15
Sign up to request clarification or add additional context in comments.

1 Comment

oh I see it now, I didn't know that I can even replace the binary operators as words in preprocessor. Thank you so much for your kind explanation.
1

The macro FUNCTION is defining a function with the name func. For example:

FUNCTION(minimum, <)

Would define a function like so:

void minimum(int& a, int x) {
 if(!(a < x)) {
 a = x;
 }
}

Macros are a method of code generation. The author of this code obviously felt it was too arduous to write out this 2 line function and instead confuse everyone with macros.

This is definitely a case where macros harm (readability) rather than help. I don't recommend doing this for such basic functions.

answered May 20, 2016 at 17:17

3 Comments

He's also confused everyone with terrible function naming & semantics. I would never expect this function to mutate an arbitrary selection of its arguments.
Hello Colin, thank you for your help. I can certainly see it better with your rephrase :)
Hello again Lightness, it was actually part of the questions in Hackerrank so wanted you to know that I didn't want to confuse everybody :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.