What is the difference between the arguments in:
int foo1(const Fred &arg) {
...
}
and
int foo2(Fred const &arg) {
...
}
? I don't see this case covered in the parashift FAQ.
There is no semantic difference between const T&
and T const&
; the language treats them as the same type. (The same thing applies to const T*
and T const*
.)
Regarding which you should prefer stylistically, however, I'll dissent from a lot of the other answers and prefer const T&
(and const T*
):
const T&
is the style used in Stroustrup's The C++ Programming Language book.const T&
is the style used in the C++ standard itself.const T*
is the style used in K&R's The C Programming Language book.const T*
is the style used in the C standard.const T&
/const T*
have way more inertia than T const&
/T const*
. const T&
/const T*
empirically seem way more common to me than T const&
/T const*
in all of the C++ and C code that I've seen. I think following common practices is more readable than dogmatically adhering to right-to-left parsing rules.T const*
, it seems easier to misplace the *
as T* const
(especially if people aren't as accustomed to it). In contrast, const* T
is not legal syntax.Regarding the whole right-to-left parsing argument that people seem to love to use: as I mentioned in a comment to another answer, const T&
reads fine right-to-left too. It's a reference to a T constant. "T" and "constant" each can work as an adjective or a noun. (Additionally, reading T const*
right-to-left can be ambiguous since it could be incorrectly interpreted as "pointer constant to T" instead of as "pointer to constant T".)
const T&
reads better, is that: 1- We read code from left to right. And 2- When describing a new concept we start from the more general concept to the more specific. Here, the const
keyword is more general as it sections the variable space into two categories, whereas the type specifier sections it into many.
T const*
No difference as const is read right-to-left with respect to the &, so both represent a reference to an immutable Fred instance.
Fred& const
would mean the reference itself is immutable, which is redundant; when dealing with const pointers both Fred const*
and Fred* const
are valid but different.
It's a matter of style, but I prefer using const
as a suffix since it can be applied consistently including const member functions.
std::is_const<const T&>::value
be false
?
const T
; the reference is mutable.
Commented
Mar 15, 2019 at 20:55
T& const
being redundant? It was a very valid question about is_const
, and one I also would like to know the answer to.
Commented
Dec 21, 2020 at 21:40
const
qualifiers on the reference itself: "Reference types cannot be cv-qualified at the top level; there is no syntax for that in declaration, and if a qualification is added to a typedef-name or decltype specifier, or type template parameter, it is ignored." (See cppreference.com)
Commented
Jan 18, 2021 at 4:00
Though they are one and the same, to retain consistency with the RIGHT-LEFT rule about parsing C and C++ declarations, it is better to write Fred const &arg
Also refer this for developing more understanding about declarations, qualifiers and declarators.
typedef
expansion. Example: typedef int* pointer;
, const pointer
is not const int*
, it's int* const
. The suffix form is not awkward.
Commented
Sep 12, 2010 at 16:49
const T&
reads fine right-to-left too; it's a reference to a T constant. T
and constant
each can work as an adjective or a noun.
Commented
Sep 12, 2010 at 18:08
Both work, and here is the explanation from the man who wrote it.
To quote him:
Why? When I invented "const" (initially named "readonly" and had a corresponding "writeonly"), I allowed it to go before or after the type because I could do so without ambiguity.
References doesn't work the same way as pointers: for pointers you can have 'const pointers' (type * const p
) and 'pointer to const' (const type * p
or type const * p
).
But you don't have this for references: a reference will always refer to the same object; in that sense you can consider that 'references' are 'const references' (the same way you can have 'const pointers').
Therefore something like 'type & const ref' is not legal. You can only have 'reference to type' (type &ref
) and 'reference to constant type' (const type &ref
or type const &ref
; both are exactly equivalent).
One last thing: even if const type
sounds more correct in English, writing type const
allows a more systematic understanding of declarations "right to left" : int const & ref
can be read has 'ref is a reference to a constant int'. Or more complicated example: int const * const & ref
, ref is a reference to a constant pointer to a constant int.
Conclusion: in your question, both are exactly equivalent.
Fred const &arg
overFred const& arg
? I like the latter better becauseconst&
is a unit there meaning "constref", and the namearg
is separated by a blank from all the type specifiers.int const& ref
doen't mean 'const ref' but 'ref to const'.