0
foo(const T& t);

vs

foo(T const& t);

As far as I know, exactly the same. The difference is purely a style thing. From what I can gather the former is a more 'old school' approach, whilst the later is what you might see more often in newer books.

Is there any difference, superficially and/or getting down into the gritty detail?

Is there any reason to choose one over the other, besides sticking to a convention within a code-base?

thecoshman
  • 8,394
  • 8
  • 55
  • 77
  • There's a difference in some cases (from what I remember, something when using templates) but I can't remember exactly. – Luchian Grigore Jul 15 '13 at 12:36
  • Also, if we're talking sytle: you can express the intent of the reference qualifier better if you write `const T &var`, since like the pointer qualifier, it qualifies the object, not the type. –  Jul 15 '13 at 12:37
  • ¬_¬ god damn question searching, the closest I got was comparing T& to T&& – thecoshman Jul 15 '13 at 12:42
  • @Luchian I think that's about macros. If you have type with a name `T`, `const T` will always be top-level const. If, however you have a macro argument `T` that is a type, `const T` can be non-top-level const (try `int*` as T in both cases and see). `T const`, however, will be top-level const in both cases. (Though as usual, macros are discouraged for many reasons and this is yet another one) – R. Martinho Fernandes Jul 15 '13 at 12:42
  • 1
    I prefer the reasons from Josuttis & Vandervoorde: http://books.google.de/books?id=yQU-NlmQb_UC&pg=PA3&lpg=PA3&dq=some+remarks+about+programming+style&hl=de – Jan Herrmann Jul 15 '13 at 12:43

1 Answers1

3

They're exactly the same. The latter is more consistent because const always appears after the thing it modifies.

If const appears before the end of the type-specifier sequence, it's pushed to the end of it. Simple as that.

Of course, English puts adjectives before the modified item. Of course, C++ isn't English (and not everyone speaks English anyway).

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421