Given a template and a more specialized overload:
template <typename T>
const T& some_func(const T& a, const T& b)
{
std::cout << "Called base template\n";
return (a < b) ? a : b;
}
template <typename T>
T* const& some_func(T* const& a, T* const& b)
{
std::cout << "Called T* overload\n";
return (*a < *b) ? a : b;
}
Then the following works as expected:
int main()
{
std::cout << some_func(5.3, 6.2) << "\n";
double a = 1;
double b = 2;
double *p = &a;
double *q = &b;
const double *ret = some_func(p, q);
std::cout << *ret << "\n";
return 0;
}
With the first printing Called base template, the second printing Called T* overload. If we replace the overload signature with:
template <typename T>
const T*& some_func(const T*& a, const T*& b)
then the second call now calls the base template. Given that int const& x is equivalent to const int& x, am I incorrect in assuming T* const& is equivalent to const T*&? Why is the first version resolved properly while the second is not?