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

前端 未结 15 1677
萌比男神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:19

    The most important thing is consistency. If there aren't any coding guidelines for this, then pick one and stick with it. But, if your team already has a de facto standard, don't change it!

    That said, I think by far the more common is

    const int* i;
    int* const j;
    

    because most people write

    const int n;
    

    instead of

    int const n;
    

    A side note -- an easy way to read pointer constness is to read the declaration starting at the right.

    const int* i; // pointer to an int that is const
    int* const j; // constant pointer to a (non-const) int
    int const* aLessPopularWay; // pointer to a const int
    
    0 讨论(0)
  • 2020-12-08 05:23

    Personally I (and it is a personal preeference) I finding reading type declarations from right to left the easiest. Especially when you start throwing references into the mix.

    std::string const&  name = plop; // reference to const string.
    
    const std::string&  flame =plop; // reference to string const;
                                     // That works better if you are German I suppose :-)
    
    0 讨论(0)
  • 2020-12-08 05:26

    I was at a conference where Bjarne Stroustrup was giving a presentation, and he used something like const int* i. Someone asked him why does he use this style and he responded (paraphrasing):

    "people like to see const first when something is constant."

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

    In modern C++11, you could also use template typedefs to add clarity to complicated declarations:

    template<typename T>
    using Const = const T;
    
    template<typename T>
    using Ptr = T*;
    

    The three declarations you mentioned in your question:

    int const * a;
    int * const b;
    int * const * c;
    

    would then be written as:

    Ptr<Const<int>> a;
    Const<Ptr<int>> b;
    Ptr<Const<Ptr<int>>> c;
    
    0 讨论(0)
  • 2020-12-08 05:29

    I hope this explanation from B. Stroustrup's FAQ on Style & Techniques will give you a definite answer.

    Bjarne Stroustrup's C++ Style and Technique FAQ


    I personaly prefer:

    int const* pi;
    int* const pi;
    

    Because const identifies the left token which is intended to be const.

    And you definitely keep the same consistency when using smth like that:

    int const* const pi;
    

    Instead of writing inconsistently:

    const int* const pi;
    

    And what happens if you have a pointer to pointer and so on:

    int const* const* const pi;
    

    Instead of:

    const int* const* const pi;
    
    0 讨论(0)
  • 2020-12-08 05:31

    Let me put my 2¢ into the discussion.

    In modern C++ I'd recommend to stick to int const instead of const int to highlight the difference between const and constexpr. It would help programmer to remember that const int cannot be always thought as compile-time constant, because:

    • compiler may (will?) allocate memory for it,
    • this memory may even be not write-protected (stack segment),
    • such a constant cannot serve all the duties constexpr can.
    0 讨论(0)
提交回复
热议问题