What is argument evaluation?

后端 未结 2 452
我寻月下人不归
我寻月下人不归 2021-01-03 18:40

Herbert Schildt says:

In some situations, real function should be used in place of function-like-macro, for example: where code size is to be minimize

2条回答
  •  情深已故
    2021-01-03 19:25

    Let's take a macro to calculate the maximum of two values:

    #define MAX(a, b) ((a) < (b) ? (a) : (b))
    

    Then we use it like this:

    int x = 5;
    int y = 10;
    int max = MAX(x++, y++);
    

    Then the macro is expanded to

    int max = ((x++) < (y++) ? (x++) : (y++));
    

    As you can see, the increment operation on either x or y will happen twice, not what would happen if you had a function where each argument you pass is evaluated only once.


    Another important point is the use of parentheses in the macro. Let's take another simple macro:

    #define MUL(a, b) a * b
    

    Now if you invoke the macro as

    int sum = MUL(x + 3, y - 2);
    

    then the expansion becomes

    int sum = x + 3 * y - 2;
    

    Which due to operator precedence is equal to

    int sum = x + (3 * y) - 2;
    

    Often not quite what was expected, if one expects (x + 3) * (y - 2).

    This problem is also "solved" by using functions.

提交回复
热议问题