73

Is there a difference between the following declarations?

void somefunc(const Person &p);
void somefunc(Person const &p);
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • 8
    You should note that the reference is irrelevant to the question: "const Person p" is equivalent to "Person const p" as well. – Evan Teran Jun 23 '09 at 23:43

5 Answers5

92

there is no difference. const binds to the type to its left...unless it is the first part of the declaration in which case it binds to the right.

See: https://isocpp.org/wiki/faq/const-correctness#const-ref-alt

Personally, I find that const T &x reads better. According to this, Bjarne also prefers to put the const first. Specifically because the keyword was originally going to be called readonly and readonly int x reads better :-P.

gideon
  • 19,329
  • 11
  • 72
  • 113
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • 1
    Though I would say the first is more common. – rlbond Jun 23 '09 at 23:35
  • @rlbond: More common depends on a context. In certain places I see the first more common in others I see the second more common. It all depnds. – Martin York Jun 23 '09 at 23:39
  • 6
    certainly the second is more *consistant* when dealing with indirection. for example: int const *const x; (everything binds left) – Evan Teran Jun 23 '09 at 23:40
  • Question about your statement "const binds to the type to its left...binds to the right". Is this left/right binding rule a formal concept described by the C++ Standard? I'm trying to understand the C++ Standard which is a bit difficult to read and can't find anything describing such a concept. (I'm just looking for help whilst trying to understanding the C++ Standard) – JavaMan Nov 26 '13 at 16:31
  • @JavaMan: unfortunately it's not quite as simple as a paragraph saying so. In the back of the standard there is a "Grammar Summary". Which specifies the language at a grammar level. Which includes specifying what contexts cv-qualifiers are valid. – Evan Teran Nov 27 '13 at 05:58
  • `const T &x` actually reads the worst, `T const& x` reads the best – jcarpenter2 Mar 17 '17 at 23:06
  • @jcarpenter, which reads best or the worst is a matter of personal opinion, which of course differs from person to person. – Evan Teran Mar 20 '17 at 16:54
47

Yes there is! the first one is more readable :)

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
  • i guess you want to say "more readable to the ones that read LTR!" :) i heard some folks in the eastern read their text RTL. – Johannes Schaub - litb Jun 24 '09 at 07:59
  • 2
    No! The second! (Really - you have to read definitions right-to-left: Person const* volatile* p; // p is a volatile pinter to a cont pointer to a Person. ) – MSalters Jun 24 '09 at 08:48
  • 1
    @litb :: I read one of those RTL languages and it is not hard because it is my native language :D – Khaled Alshaya Jun 24 '09 at 13:55
11

See this http://www.parashift.com/c++-faq-lite/const-correctness.html

(See new link instead : https://isocpp.org/wiki/faq/const-correctness#const-ref-alt)

Specifically 18.6 to 18.8.

[18.6] What does "const Fred& x" mean?

It means x aliases a Fred object, but x can't be used to change that Fred object.

[18.7] Does "Fred& const x" make any sense?

No, it is nonsense.

[18.8] What does "Fred const& x" mean?

Fred const& x is functionally equivalent to const Fred& x. However, the real question is which should be used.

Read the rest of the article for more info. But essentially, it says they are equivalent, and you can use either one as you see fit. However, you should pick one and stick with it, to avoid future confusion.

gideon
  • 19,329
  • 11
  • 72
  • 113
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • 4
    I'll add, pick one and stick with it when writing new code, but when modifying existing code, stick with the way the existing code does it. – Rob K Jun 24 '09 at 18:37
4

Both are the same.

const Person & x is currently the most popular, because English puts adjectives before the nouns, and that makes it "more English like."

The alternative form, Person const & x, was suggested as a less idiomatic form. Their argument was that that form can always be parsed from right to left, "x is a reference to a const Person." The argument was that clarity of parsing was more important than idiomatic structure.

Unfortunately for those people, their form doesn't handle everything. Once you start seeing arrays and function pointers and other exotic forms, the rules start to break down. char *str[10] cannot be written in right to left form because the array has to be on the right side. void (*signal(int, void (*fp)(int)))(int) makes the right to left people cringe in pain.

Only rule has ever cropped up to make sense of all types, the Clockwise Spiral rule: http://c-faq.com/decl/spiral.anderson.html

Usually, once someone sees the the clockwise spiral, all hopes of a a clear parsing rule goes out the window. Most people then accept that const Person& x is probably the best way to go!

Cort Ammon
  • 10,221
  • 31
  • 45
3

It really is a matter of taste.

If read from right to left, "Person const & x" reads "x is a reference to a constant Person."

This sounds better than "const Person & x", which would be "x is a reference to a Person, which is constant."

So if one is familiar with the right-to-left reading direction of variable declarations, one perhaps would prefer the first one.