tags:

views:

335

answers:

5

For example, is

int const x = 3;

valid code?

If so, does it mean the same as

const int x = 3;

?

A: 

(Edit:)

I thougth that the first isn't valid Code but this is not the case.

It means the same.

Quonux
The first **is** valid code.
JAB
wtf, but i saw the syntax never and it is just unlogicaly.
Quonux
Just because you've never seen something doesn't mean that it doesn't exist, or that it's wrong... :-)
stakx
@Quonux - Check out http://stackoverflow.com/questions/988069/in-c-is-const-after-type-id-acceptable . I (and many others) contend the reverse is actually "unlogicaly".
T.E.D.
@T.E.D. Well.. I would argue that the first *is* illogical, but is more consistent with the illogical way that `const` has to be written at all other times...
BlueRaja - Danny Pflughoeft
+14  A: 

They are both valid code and they are both equivalent. For a pointer type though they are both valid code but not equivalent.

Declares 2 ints which are constant:

int const x1 = 3;
const int x2 = 3;

Declares a pointer whose data cannot be changed through the pointer:

const int *p = &someInt;

Declares a pointer who cannot be changed to point to something else:

int * const p = &someInt;
Brian R. Bondy
And ` const int * const p = ` would give you a pointer that cannot be changed to a value that cannot be changed, if I'm not mistaken.
JAB
@JAB: Yes it will give you a pointer that can't be changed in addition to having data that can't be changed inside the pointer through that pointer.
Brian R. Bondy
The pattern here becomes apparent if the definitions are **read in reverse**. For example: `[const int *]` = a pointer (`*`) to an `int` that is `const`. `[int * const]` = a `const` pointer (`*`) to an `int`.
stakx
C syntax reads crappy no matter what you do. It wasn't designed to produce readable sources. You just have to learn the rules.
T.E.D.
@ T.E.D. : Not quite. Just get into the habit to write `const` always *after* the thing it applies to, and you're done. (Always-trailing is the only style that could be applied *consistently*.)
DevSolar
You should also include 'int const *' and just for a laugh 'int const * const'
Martin York
@Martin: confusing enough already for some :)
Brian R. Bondy
@Brian R. Bondy - They can both be quite useful at times though. In particular I end up using `int const *` (a read-only view of some other variable or area of memory) in systems programming all the time.
T.E.D.
+5  A: 

Yes, that is exactly the same. However, there is difference in pointers. I mean:

int a;

// these two are the same: pointed value mustn't be changed
// i.e. pointer to const value
const int * p1 = &a;
int const * p2 = &a;

// something else -- pointed value may be modified, but pointer cannot point anywhere else
// i.e. const pointer to value
int * const p3 = &a;

// ...and combination of the two above
// i.e. const pointer to const value
const int * const p4 = &a;
Archie
+1  A: 

It is the same in meaning and validity.

As far as I know, const only get complex whenever it involves pointer.

int const * x;
int * const x; 

are different.

int const * x;
const int * x; 

are same.

ttchong
+12  A: 

Yes, they are the same. The rule in C++ is essentially that const applies to the type to its left. However, there's an exception that if you put it on the extreme left of the declaration, it applies to the first part of the type.

For example in int const * you have a pointer to a constant integer. In int * const you have a constant pointer to an integer. You can extrapolate this to pointer to pointers, and the English may get confusing but the principle is the same.

For another dicussion on the merits of doing one over the other, see my question on the subject. If you are curious why most folks use the exception, this FAQ entry of Stroustrup's may be helpful.

T.E.D.
Exactly what I was thinking. `const int x` is the exceptional form. `int const x` fits the same pattern as `int * const x`. If you need a constant pointer to a constant int, I usually write it as `int const * const` for consistency rather than `const int * const`.
Cogwheel - Matthew Orlando
@Cogwheel - Precisely. Once you realise the "normal" form of putting the `const` to the extreme left is actually making use of an exceptional case, the question arises as to which way actually makes the code clearer in the long run. Hence my question http://stackoverflow.com/questions/988069/in-c-is-const-after-type-id-acceptable
T.E.D.