-7

I have one very interesting question about preprocessor directives in c++.

Consider the following macros and his usage:

    #define FUNCTION(a, b) void (a)(int &current, int candidate)\
    {\
        if ((current b candidate) == false){\      // Marked Line    
            current = candidate;\
        }\
    }

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

My question is why changing the "Marked Line" with the following line of code won't even compile:

     ... if ((current (b) candidate) == false) ...

1 Answers1

2

Because '<' is a binary operator and it cannot be evaluated without an operand on either side. You can verify this without macros simply by attempting to compile the following code:

bool LessThan( int a, int b )
{
    return( a (<) b );
}

At the very least you should see "expected an expression" or a similar error.

Tim Randall
  • 4,040
  • 1
  • 17
  • 39
  • Ok. I understood it but what is the idea of putting the macros argument in parentheses so? – Tony Troeff Jul 24 '18 at 16:36
  • 1
    See [this question/answer](https://stackoverflow.com/questions/7186504/c-macros-and-use-of-arguments-in-parentheses) – Kevin Jul 24 '18 at 16:47
  • 2
    Normally people advise you to put arguments in parentheses because the macro preprocessor does very simple, literal text substitutions that can lead to incorrect code. The advice breaks down when a macro argument is an operator rather than an expression. – Tim Randall Jul 24 '18 at 16:54
  • Thank you, Tim. I appreciate your help! – Tony Troeff Jul 24 '18 at 18:01