Why is f(i = -1, i = -1) undefined behavior?

前端 未结 11 1664
再見小時候
再見小時候 2020-12-04 05:28

I was reading about order of evaluation violations, and they give an example that puzzles me.

1) If a side effect on a scalar object is un-sequenced r

11条回答
  •  天命终不由人
    2020-12-04 06:22

    The assignment operator could be overloaded, in which case the order could matter:

    struct A {
        bool first;
        A () : first (false) {
        }
        const A & operator = (int i) {
            first = !first;
            return * this;
        }
    };
    
    void f (A a1, A a2) {
        // ...
    }
    
    
    // ...
    A i;
    f (i = -1, i = -1);   // the argument evaluated first has ax.first == true
    

提交回复
热议问题