Why do some people prefer “T const&” over “const T&”?

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).

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).

UncleBens, thanks. So it seems T const& is preferred by those who want to read it right-to-left like the compiler, and const T& is preferred by those who want to read it left-to-right like English. – Michael Aaron Safyan Apr 14 '10 at 19:57 Actually English often goes from right to left, e.

G a of be 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 Apr 14 '10 at 20:53 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 Apr 14 '10 at 23:49 @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 Apr 14 '10 at 12:10.

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 *.

– Michael Aaron Safyan Apr 14 '10 at 23:52 @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 Apr 15 '10 at 3:45.

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

That's because some find it helpful to read the declaration right-to-left. Char const* const char* are both pointer to const char.

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).

2 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 Apr 14 '10 at 19:58 @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 Apr 14 '10 at 20:24 @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 Apr 14 '10 at 20:33 @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 Apr 14 '10 at 20:36 2 @Adisak: I see your point, and admit I worded it wrong.

That was not my intent. Fixed my answer for you :) – Corey Apr 14 '107 at 4:11.

My reasoning is as follows: It does seem to roll off the tongue better if you write "const T&" but when you do that you end up with the ambiguous, "constant T reference. " I've seen this cause problems more than once in the understandability of code that allowed someone, even semi-experienced, to misinterpret what something meant or how to declare a more complex type. I can't think of any example right now but more than once I've answered questions about type declarations and constness where the problem was caused by the habit of using "const T &" instead of "T const &".

I used to write it that way as well and when I became a Sr. Developer, someone in charge of mentoring and creating code standards in projects, I found it much easier for entry level developers when I force everyone to use "T const&". I suppose one rather trivial, rookie mistake would be why does this code compile?

Const T* t = f(); t = 0; // assignment to const? - no, it is not the T* that is const, just the T. When you learn to read it the way that the compiler does it becomes much easier to understand what any given complex type is as well as allowing you to more readily declare complex types when you need to.By complex types I'm talking about things such as: T const * const & When you know that the compiler reads right to left, inside to out, what that means becomes quite apparent and when it is necessary to write one you can do so easily: reference to constant pointer to a constant T.

Now write the declaration of a "reference to a pointer to a constant pointer to a T". If you simply use left to right notation I think you'll find this quite easy.In short, though it initially seems unnatural teaching oneself to use right->left grammar ALL the time, instead of only when it is required (because it often is), you'll find it much easier to remember what a line of code means and how to write what you mean. It's sort of along the same lines of why I disallow "," in declarations: T* ptr1 = 0, ptr2 = 0; // oops!

// do it this way please! T* ptr1 = 0; T* ptr2 = 0; Technically it's all the same but when you try to get a bunch of people of varying capacities working on the same thing you'll tend to make sure everyone uses whatever method is the easiest to understand and use. My experience has taught me that "T const&" is that method.

The compiler reads left to right" - it seems you swapped directions there. – Georg Fritzsche Apr 14 '10 at 22:22 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 Apr 14 '10 at 23:55 @gf lol!

Yes, I did. Editing... – Crazy Eddie Apr 15 '10 at 16:02.

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.

1 +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 May 28 '10 at 16:03 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 means const void *foo; – Larry Engholm May 28 '10 at 16:46.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions