Search code examples
c++macrospreprocessor-directive

Why putting a macros argument in parentheses leads to an error?


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) ...

Solution

  • 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.