mario::konrad
programming / C++ / sailing / nerd stuff
C++ Style: 'const' before or after the type
© 2013 / Mario Konrad

Is it better to write the const keyword before or after the data type. Although the style:

const int * a = &b;

is more pleasing to my eyes, the other way arround (writing const after the type) is more consistent:

int const * a = &b;

This way, everything left of const is meant to be const. Example:

1
2
3
4
5
6
7
8
9
10
11
12
int main(int, char**)
{
    int a = 10;

    int const * a0 = &a; // pointer to const int

    int * const a2 = &a; // const pointer to int

    int const * const a1 = &a; // const pointer to const int

    return 0;
}