The result of int c=0; cout<<c++<<c;

后端 未结 5 1905
无人及你
无人及你 2020-12-06 19:29

I think it should be 01 but someone says its \"undefined\", any reason for that?

5条回答
  •  青春惊慌失措
    2020-12-06 20:14

    The behavior is defined but unspecified. The relative order of evaluating the two uses of 'c' in the expression isn't specified. However, if you convert it to functional notation, it looks like this:

    cout.operator<<(c++).operator<<(c);
    

    There's a sequence point between evaluating the arguments to a function, and executing the body of the function, and function bodies aren't interleaved, so the result is only unspecified, not undefined behavior.

    If you didn't have an overloaded operator:

    int c=0;
    int a = c++ << c;
    

    Then the behavior would be undefined, because both modifying and using the value of c without an intervening sequence point.

    Edit: The sequence litb brings up is simply wrong. The standard specifies (§1.9/17): "When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body."

    This clearly written with the idea that arguments are evaluated, then (immediately afterward) the body of the function is executed. The sequence he suggests, in which arguments to one function are evaluated, then arguments to another, then execution of both function bodies doesn't seem to have been intended, but also isn't prohibited. That, however, changes nothing -- the requirement is still that: "...there is a sequence point after the evaluation of all function arguments (if any)..."

    The subsequent language about execution of the body does NOT remove the requirement for a sequence point after evaluating all function arguments. All other evaluation, whether of the function body or other function arguments follows that sequence point. I can be as pedantic and perverse as anybody about mis-reading what's clearly intended (but not quite stated) -- but I can't imagine how "there is a sequence point after the evaluation of all function arguments" can be read as meaning "there is NOT a sequence point after the evaluation of all function arguments."

    Neil's point is, of course, correct: the syntax I've used above is for member functions. For a non-member overload, the syntax would be more like:

    operator<<(operator<<(cout,c++), c);
    

    This doesn't remove the requirement for sequence points either though.

    As far as it being unspecified: it's pretty simple really: there's a sequence point after evaluating all function arguments, so all the arguments for one function call must be fully evaluated (including all side effects), then arguments for the other function call can be evaluated (taking into account any side effects from the other) -- BUT there's no requirement about WHICH function call's arguments must be evaluated first or second, so it could be c, then c++, or it could be c++, then c -- but it has to be one or the other, not an interleaving.

提交回复
热议问题