const int *p vs. int const *p - Is const after the type acceptable?
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;
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 const
ness 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
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
.