Sequence Point ambiguity, undefined behavior?

后端 未结 2 1229
南方客
南方客 2021-02-12 13:44

Today I came across some code that exhibits different behavior on clang++ (3.7-git), g++ (4.9.2) and Visual Studio 2013. After some reduction I came up with this snippet which h

2条回答
  •  清歌不尽
    2021-02-12 14:04

    Well, no, it's not a case of undefined behaviour. It is a case of unspecified behaviour.

    It is unspecified whether the expression len_ = len will be evaluated before or after buffer(len+1). From the output you have described, g++ evaluates buffer(len+1) first, and clang evaluates len_ = len first.

    Both possibilities are correct, since the order of evaluation of those two sub-expressions is unspecified . Both expressions will be evaluated (so the behaviour does not qualify as being undefined) but the standard does not specify the order.

提交回复
热议问题