Search code examples
c++functiontemplatesimplicit-conversionoverload-resolution

C++ Overload Resolution, userdefined conversion and function template


With g++ 3.4 and 4.7 I observed the following strange behavior:

A function template does not match if a user defined conversion is necessary, where a plain function will. I could not find the corresponding rule in the C++98 Standard. Is g++ correct, (as I assume)? Or is it a bug?

template <class T>
int x(auto_ptr_ref<T> p)
{
  return 1;
}
// this would match
/*
int x(auto_ptr_ref<int> p)
{
   return 2;
}
*/
void dummy()
{
  cout << x(auto_ptr<int>()) << endl;
}

Solution

  • GCC is correct, template argument deduction doesn't consider implicit conversions.

    Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens later.

    For your code, auto_ptr_ref doesn't match against auto_ptr, the deduction of template parameter T fails, so the function template x() won't be considered for overload resolution at all.