Say I have a class template with some specializations
template<typename T> struct A {};
template<typename T> struct A<T const> {};
template<typename T> struct A<T &> {};
Which specialization has the precedence, when I instantiate A<const int&>
?
I ran a test with GCC, and it picks the int&
specialization. I assume this is the rule, but I can't find where this is stated.
Edit: I was making a mistake considering that const int&
matches const T
for T=int&
, since templates are more than a mere "macro substitution". In particular, it makes no sense to talk about a const reference (only reference to const). Both the current answers helped me figure it out. Unfortunately, I can only pick one as accepted answer, but thank you both.
X&
and const Y
can’t ever be decompositions of the same type, since references can’t be const-qualified. (Given using R=T&;
, const R
is the same type as R
, but that doesn’t mean that it has that qualifier to match anything.) As such, no ordering is needed, since the two specializations are disjoint.