views:

492

answers:

7

So, I realize that const T& and T const& are identical and both mean a reference to a const T. In both cases, the reference is also constant (references cannot be reassigned, unlike pointers). I've observed, in my somewhat limited experience, that most C++ programmers use const T&, but I have come across a few people who use T const&. I use const T& simply because I learned it that way, and so T const& looks a little bit funny to me. What is the reason that you use the variant that you use? Do any of you work at an organization for which the coding standards mandate the use of one variant over the other?

Edit
Based on the answers, it would appear that one reason for choosing between the two is whether you want to read it like the compiler (right-to-left) or like English (left-to-right). If one reads it like the compiler, then "T const&" reads as "& (reference) const (to a constant) T (of type T)". If one reads it like English, from left-to-right, then "const T&" is read as "a constant object of type T in the form of a reference". I prefer to read it like English prose, but I can certainly see the sense in interpreting it the way that the compiler does.

No one has answered the organization or coding standards question, but I strongly suspect that most organizations do not mandate one over the other, although they might strive for consistency.

+3  A: 

I think is personal preference. There is no difference between the two variants.

Cătălin Pitiș
+11  A: 

I think some people simply prefer to read the declarations from right to left. const applies to the left-hand token, except when there is nothing there and it applies on the right-hand token. Hence const T& involves the "except"-clause and can perhaps be thought more complicated (in reality both should be as easy to understand).

Compare:

const T* p;  (pointer to T that is const)
T const* p;  (pointer to const T) //<- arguable more natural to read
T* const p;  (const pointer to T)
UncleBens
Michael Aaron Safyan
Actually English often goes from right to left, e.g `a of b` whereas in other languages only `b's a` might be possible. (Which way things are natural to read is deeply subjective, perhaps also depending on language background, how analytic/synthetic your way of thinking is etc)
UncleBens
@UncleBens, I take your meaning, but "a of b" is still read from left-to-right (you say {"ay", "of", "bee"} and not {"bee", "of", "ay"}), even though it is evaluated from right-to-left.
Michael Aaron Safyan
@Michael, albeit writing "T const" i still read declarations from left-to-right. So i say "T const" instead of "const T".
Johannes Schaub - litb
+1  A: 

Being as code is predominantly English-based, programmers tend to read left to right, so const T& reads naturally to us where the compiler reads it inside out right to left so T const& reads naturally(reference to a const T)

Bruce
Humans don't tend to read left to right - the preference is purely cultural. A sizable portion of the population read their native languages vertically (i.e. Chinese and Japanese) or right to left (i.e. Arabic and Hebrew). Have you ever bought a Japanese comic book and noticed that the pages are in "backwards" order (i.e. the spine is on the right and not the left) ?
Adisak
@Adisak: I think he meant tend as in "people who write code tend", most code writers read left-to-right since it's predominately English-based.
GMan
@Adisak Yes, you are right, but "const" is not a Chinese or Japanese word, it's a shortening of the English word "constant" Neither are the keywords "if", "while", "static", etc. The C++ language is tied to an English heritage. If a language exists where all the keywords are primarily Chinese characters, it would make sense to talk about its natural "right to left" reading. But then someone would argue that interpretation was primarily cultural, and humans don't tend to read right to left. Perhaps humans are flexible enough to tend to read in the direction of the language, not vice versa.
Edwin Buck
@GMan, but there are lots of coders in Israel, and the Technion is quite famous. I think Bruce meant to write "English speakers", and his point is otherwise valid. Adisak's criticism is likewise valid.
Michael Aaron Safyan
@GMAN: Yes pretty much all code is left to right. But that again is cultural conditioning (as you state, code is predominantly English-based), not human tendancy. Let's say in an alternate past, the Arab world had remained a center of knowledge and invented computers -- There is no reason that if history were different we wouldn't be reading right-to-left and writing code right-to-left. My correction is that it's not a generic "Human" tendency, but rather a cultural one (and programming is a culture as well).
Adisak
@Adisak: I see your point, and admit I worded it wrong. That was not my intent. Fixed my answer for you :)
Bruce
+2  A: 

That's because some find it helpful to read the declaration right-to-left.

char const*
const char*

are both pointer to const char.

JRL
+8  A: 

This will make a difference when you have more then one const/volatile modifiers. Then putting it to the left of the type is still valid but will break the consistency of the whole declaratiion. For example:

T const * const *p;

means that p is a pointer to const pointer to const T and you consistenly read from right to left.

const T * const *p;

means the same but the consistency is lost and you have to remember that leftmost const/volatile is bound to T alone and not T *.

Tomek
It might also make a difference if someone does `T const` and then someone else comes along who does `const T`, and accidentally changes something to say: `const T const * const p`, to which the compiler replies `error: Just how const do you want it to be?`
greyfade
@greyfade, haha, I had heard about that error, though have never seen it in person... which compiler is it?
Michael Aaron Safyan
@Michael Aaron Safyan: I think I managed to get GCC to spit it out once, but I honestly don't remember and haven't reproduced it.
greyfade
+1  A: 
Noah Roberts
*"the compiler reads left to right"* - it seems you swapped directions there.
Georg Fritzsche
When I can vote again, I will give you a +1 for one declaration per line, and for answering the question about whether anyone has mandated the use of one variant over the other.
Michael Aaron Safyan
@gf lol! Yes, I did. Editing...
Noah Roberts
+1  A: 

If you find this discussion interesting, you'd probably find this article by Dan Saks interesting. It doesn't directly address your question, but explains why he prefers

VP const foo[];

to

const VP foo[];

It's because given

typedef void *VP;

you could easily be misled into thinking that the second example above means

const void *foo[];  // Wrong, actually: void *const foo[];

but the first example is harder to misinterpret.

Larry Engholm
+1 for the link to the article, but I think You got it backwards in Your post. Both "VP const foo[];" and "const VP foo[];" actualy do mean "void *const foo[];". Check the article again.
Maciej Hehl
Thanks for the correction. You'd be led into thinking that the first example means what it actually means (which is why he prefers it), and misled into thinking that the second example meansconst void *foo[];
Larry Engholm