C++ 重载解析、用户定义转换和函数模板
C++ Overload Resolution, userdefined conversion and function template
对于 g++ 3.4 和 4.7,我观察到以下奇怪的行为:
如果需要用户定义的转换,则函数模板不匹配,而普通函数会。
我在 C++98 标准中找不到相应的规则。 g++ 是否正确(正如我假设的那样)?或者这是一个错误?
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;
}
GCC 是正确的,template argument deduction 不考虑隐式转换。
Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens later.
对于您的代码,auto_ptr_ref
与 auto_ptr
不匹配,模板参数 T
的推导失败,因此函数模板 x()
不会完全考虑过载解决方案。
对于 g++ 3.4 和 4.7,我观察到以下奇怪的行为:
如果需要用户定义的转换,则函数模板不匹配,而普通函数会。 我在 C++98 标准中找不到相应的规则。 g++ 是否正确(正如我假设的那样)?或者这是一个错误?
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;
}
GCC 是正确的,template argument deduction 不考虑隐式转换。
Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens later.
对于您的代码,auto_ptr_ref
与 auto_ptr
不匹配,模板参数 T
的推导失败,因此函数模板 x()
不会完全考虑过载解决方案。