When the same function template specialization can be generated from more than one declaration, the declarations are disambiguated using partial ordering of function templates as described in the C++11 standard §14.5.6.2 Partial ordering of function templates [temp.func.order]. The compiler determines which of the templates is the most specialized and prefers it.
In your example, the const T&
overload of f
is more specialized than the T&&
overload. Intuitively, T&&
can be deduced to anything that const T&
can, but not vice versa, so const T&
is more specific and therefore its function overload is more specialized.