Why is this undefined behaviour?

前端 未结 3 1502
[愿得一人]
[愿得一人] 2021-01-21 17:36

Here\'s the sample code:

X * makeX(int index) { return new X(index); }
struct Tmp {
    mutable int count;
    Tmp() : count(0) {}
    const X ** getX() const {          


        
3条回答
  •  盖世英雄少女心
    2021-01-21 18:11

    Because it this case, the , is NOT a sequence point, but acts more like a delimiter in the initialization of the elements of the array.

    In other words, you're modifying the same variable twice in a statement without sequence points (between the modifications).


    EDIT: thanks to @MikeSeymour: this is an issue in C++03 an before. It seems like in C++11, the order of evaluation is defined for this case.

提交回复
热议问题