Order of evaluation in C++ function parameters

后端 未结 6 873
野性不改
野性不改 2020-11-22 00:41

If we have three functions (foo, bar, and baz) that are composed like so...

foo(bar(), baz())

Is there any guarantee by the C++ standard th

6条回答
  •  自闭症患者
    2020-11-22 01:10

    From [5.2.2] Function call,

    The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations take effect before the function is entered.

    Therefore, there is no guarantee that bar() will run before baz(), only that bar() and baz() will be called before foo.

    Also note from [5] Expressions that:

    except where noted [e.g. special rules for && and ||], the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.

    so even if you were asking whether bar() will run before baz() in foo(bar() + baz()), the order is still unspecified.

提交回复
热议问题