typedef and containers of const pointers

c++, containers, stl, typedef

Solution

Short answer:

- is a list of pointers to constant ints.

- is a list of constant pointers to ints.

- is the same as 2.

const (and volatile) should naturally appear after the type they qualify. When you write it before, the compiler automatically rewrites it internally:

const int *

becomes

int const *

which is a pointer to a constant int. Lists of these will compile fine since the pointer itself is still assignable.

Problem

The following line of code compiles just fine and behaves: ``` list<const int *> int_pointers; // (1) ``` The following two lines do not: ``` typedef int * IntPtr; list<const IntPtr> int_pointers; // (2) ``` I get the exact same compile errors for ``` list<int * const> int_pointers; // (3) ``` I'm well aware that the last line is not legal since the elements of an STL container need to be assignable. Why is the compiler interpreting (2) to be the same as (3) ?

Original source