Const Placement Style

The placement of the const qualifier on a type is generally dictated by the language, except when it is modifying the base type. When it is modifying the const-ness after a * (pointer), it must be placed after the *.


    int* const p = 0;
    int* const* const p2 = 0;

But, when modifying a base type, it may be placed on either side of it.


    int const i = 0;
    const int i2 = 0;

Both i and i2 have the same type, which is a constant integer.

The "old" style is to put const to the left of the type it modifies, as is done for i2 above. This, however, is inconsisent:


    const int i = 0;
    const int* const* p;

Here, when typing the declaration, one must remember to put the first const on the left of the type it modifies, while putting sunsequent consts to the right of the type that is modified. When reading the second declaration (from right-to-left), const usually modifies what is on the left, but we have to backtrack and apply the last const to the type "before" it.

Enter the consistent style: const modifies the type on the left, always.


    int const i = 0;
    int const* const* p;

Read from right-to-left. First one is a const int. Second one is a pointer to const pointer to const int. Simple. We're done.

But some compilers have particular trouble when this is combined with template type parameters, especially when the types already have const as their top-level qualifier. Oh well. When they catch up, we can rid ourselves completely of this inconsistent style. :-)


Blargg's C++ Notes