Why is this undefined behaviour?

前端 未结 3 1503
[愿得一人]
[愿得一人] 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:18

    In C++11, this is fine; each clause of an initialiser list is sequenced before the next one, so the evaluation is well-defined.

    Historically, the clauses might have been unsequenced, so the two unsequenced modifications of count would give undefined behaviour.

    (Although, as noted in the comments, it might have been well-defined even then - you can probably interpret the standard as implying that each clause is a full-expression, and there's a seqeuence point at the end of each full-expression. I'll leave it to historians to debate the finer points of obsolete languages.)

提交回复
热议问题