Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some people prefer "T const&" over "const T&"?

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.

like image 943
Michael Aaron Safyan Avatar asked Apr 14 '10 19:04

Michael Aaron Safyan


People also ask

What is const T&?

const T is passed by value (copy is needed), which may be too much for a class while should be ok for built-in types. T& is passed by reference, which could be modified in the function while const T& can't, so it depends on what you need to do. Follow this answer to receive notifications.

Should I always use const reference?

When writing a C++ function which has args that are being passed to it, from my understanding const should always be used if you can guarantuee that the object will not be changed or a const pointer if the pointer won't be changed.

What is the point of const?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it. In C, constant values default to external linkage, so they can appear only in source files.

What is const T & in C++?

const T* would only forbid you to modify anything the pointer points to, but it allows you (within the bounds of the language) to inspect the value at *(foo+1) and *(foo-1) . Use this form when you're passing pointers to immutable arrays (such as a C string you're only supposed to read).


2 Answers

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) 
like image 142
UncleBens Avatar answered Sep 27 '22 22:09

UncleBens


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

like image 43
Tomek Avatar answered Sep 27 '22 21:09

Tomek