What is argument evaluation?

后端 未结 2 445
我寻月下人不归
我寻月下人不归 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:31

    Sometimes arguments have side effects.

    For example, the value of i++ is i, but i is increased by 1. As a result, the value of next i++ will be i + 1.

    In a macro, arguments are evaluated every time it's called for, causing resulting values; In a function, the (actual) arguments are evaluated and copied to (formal) arguments inside the function, dismissing the side effects.

    When inplementing a function, you don't care about side effects. However, implicit type promotion and casting may be error-prone instead.

提交回复
热议问题