How to know if you can bind a const reference T1 to T2 ?
I used to think that you can bind const reference T1 to type T2 only if T2 is convertible to T1.
But since the following compiles:
char x[10];
const char (&y)[10] = x;
that should not be the case, since char[10] is not convertible to const char[10] (correct me if I'm wrong). So, what are the rules for being able to bind const references to different types ? Is there just an additional rule like: for any type T you can bind a const reference T to it ?
The standard describes the reference binding rules in [dcl.init.ref]/4 and [dcl.init.ref]/5. There is a rather long list of rules, but the bits most relevant to your question are:
[dcl.init.ref]/4:
[dcl.init.ref]/5:
In your case,
T1would beconst char [10]andT2would bechar [10].T1is reference-compatible withT2becauseT2*can be converted toT1*, as it only requires addingconst-qualification to the pointed type, which is a standard conversion.As you can see in the referenced sections, this is not the only case where reference binding is allowed - another case, for example, is binding a reference to a result of conversion (including user-defined).
constreferences are also special as they are allowed to bind to rvalues.Note that this is indeed different from your previous understanding -
T2may not be convertible toT1while you may be able to bind a reference still. Here's an example: