Search code examples
c++referenceconstants

C++: const reference, before vs after type-specifier


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.


Solution

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