const int *p vs. int const *p - Is const after the type acceptable?

前端 未结 15 1683
萌比男神i
萌比男神i 2020-12-08 05:01

My co-worker is 0 for 2 on questions he has inspired (1, 2), so I thought I\'d give him a chance to catch up.

Our latest disagreement is over the style issue of wher

15条回答
  •  萌比男神i
    2020-12-08 05:32

    If it were only variables and pointers to them that could be const or not, it would probably not matter that much. But consider:

    class MyClass
    {
        public:
            int foo() const;
    };
    

    No way that const could be written anywhere else but trailing the function it refers to.

    The shorter a style guide, the more likely developers will follow it. And the shortest rule possible, and the only rule that will give you consistency, is:

    The const keyword always trails whatever it is referring to.

    So, I'd say 0:3 for your coworker here.

    Regarding your "ultimate issue": For the sake of the style guide, it does not matter whether the guide "discourages" or "bans" the things it speaks out against. That is a social issue, a policy. The style guide itself should be as crisp and short as possible. Long style guides just get ignored by everybody (except management on the lookout for someone to blame), so just write "do" or "don't", and state what you do with violations of the guide elsewhere (e.g. in the company policy of how peer reviews are being done).

提交回复
热议问题