username_4567 username_4567 - 2 months ago 4
C++ Question

What is the difference between "const int& jj" and "int& const jj"?

I am confused with the two. I am aware of the C++ references which are inherently constant and once set they cannot be changed to refer to something else.

Answer

const int& means reference to const int. (Similarly, int& means reference to non-const int.)

int& const literally means const reference (to non-const int), which is invalid in C++, because reference itself can't be const-qualified.

$8.3.2/1 References [dcl.ref]

Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef-name ([dcl.typedef], [temp.param]) or decltype-specifier ([dcl.type.simple]), in which case the cv-qualifiers are ignored.

As you said, references are inherently constant and once set they cannot be changed to refer to something else. (We can't rebind a reference after its initialization.) This implies reference is always "const", then const-qualified reference or const-unqualified reference might not make sense actually.