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

前端 未结 15 1675
萌比男神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条回答
  • 2020-12-08 05:07

    There's a class of examples where putting the const on the right of the type also helps avoid confusion.

    If you have a pointer type in a typedef, then it is not possible to change the constness of the to type:

    typedef int * PINT;
    const PINT pi;
    

    pi still has the type int * const, and this is the same no matter where you write the const.

    0 讨论(0)
  • 2020-12-08 05:07

    I agree with both of you. You should put the const after the type. I also find looking at it an abomination that must be destroyed. But my recent foray into the wonders of const value parameters has made me understand why putting the const second makes sense.

    int *
    int const *
    int * const
    int const * const
    

    Just looking at that has the hairs on my neck standing. I'm sure it would confuse my co-workers.

    EDIT: I was just wondering about using this in classes:

    class Foo {
        Bar* const bar;
        Foo(const Foo&) = delete; // would cause too many headaches
    public:
        Foo() : bar(new Bar) {}
        ~Foo() { delete bar; }
    };
    

    bar in this example is functionally equivalent to Bar& but it is on the heap and can be deleted. For the lifetime of each Foo, there will be a single Bar associated with it.

    0 讨论(0)
  • 2020-12-08 05:10

    While there is no meaningful difference between const int and int const (and I've seen both styles in use), there is a difference between const int * and int * const.

    In the first, you have a pointer to a const int. You can change the pointer, but you can't change the value it points to. In the second, you have a const pointer to int. You can't change the pointer (hope it's initialized to your liking), but you can change the value of the pointed-to int.

    The proper comparison is with const int * and int const *, which both are pointers to a const int.

    Remember that the * doesn't necessarily work as you might like. The declaration int x, y; will work as you expect, but int* x, y; declares one pointer to int, and one int.

    0 讨论(0)
  • 2020-12-08 05:13

    People typically use const int* blah because it reads well as English. I wouldn't underestimate the usefulness of that.

    I find that the int* const blah variation is rare enough that it's not typically useful to make the more common definition backwards. I am, in general, not a fan of anything that even slightly obscures code in the general case, though it might provide some nominal benefit in the exceptional case.

    See also "if (1 == a)". Some people really enjoy writing code that doesn't read as English. I am not one of those people.

    Really, though, the rules behind const are simple. Look to the left, then to the right. So simple that I wouldn't think it's worth much attention in a style guide.

    0 讨论(0)
  • 2020-12-08 05:14

    Putting "const" after the type declaration makes a whole lot more sense once you train yourself to read your C++ type declarations from right to left.

    I'm going to peg your cow-orker at 0-for-3 :)

    0 讨论(0)
  • 2020-12-08 05:15

    The ultimate issue here is that he thinks that putting const after int is so unspeakably ugly, and so harmful to readability that it should be banned in the style guide

    Really?

    Show me a programmer who gets bogged down when he sees:

    int foo() {
    }
    

    vs

    int foo()
    {
    }
    

    ...and I'll show you a programmer who doesn't pay close enough attention to detail.

    No professional programmer worth his salt will have a problem with superficial differences in style.

    EDIT: It is true that const int* and int* const don't mean exactly the same thing, but that wasn't the point. The point made by OP's coworker was that differences in style make code difficult to understand & maintain. It is this claim I disagree with.

    0 讨论(0)
提交回复
热议问题