What's the point of g++ -Wreorder?

后端 未结 5 650
花落未央
花落未央 2020-11-29 17:49

The g++ -Wall option includes -Wreorder. What this option does is described below. It is not obvious to me why somebody would care (especially enough to turn this on by de

5条回答
  •  孤独总比滥情好
    2020-11-29 18:20

    The warning exists because if you just read the constructor, it looks like j is getting initialized before i. This becomes a problem if one is used to initialize the other, as in

    struct A {
      int i;
      int j;
      A(): j (0), i (this->j) { }
    };
    

    When you just look at the constructor, this looks safe. But in reality, j has not yet been initialized at the point where it is used to initialize i, and so the code won't work as expected. Hence the warning.

提交回复
热议问题