加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C过载分辨率,用户定义的转换和功能模板

发布时间:2020-12-16 03:35:51 所属栏目:百科 来源:网络整理
导读:使用g 3.4和4.7,我观察到以下奇怪的行为: 如果需要用户定义的转换,则函数模板不匹配,其中普通函数将是. 我在C 98标准中找不到相应的规则.是正确的,(我假设)?或者这是一个错误? template class Tint x(auto_ptr_refT p){ return 1;}// this would match/*i
使用g 3.4和4.7,我观察到以下奇怪的行为:

如果需要用户定义的转换,则函数模板不匹配,其中普通函数将是.
我在C 98标准中找不到相应的规则.是正确的,(我假设)?或者这是一个错误?

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()根本不会被考虑用于重载解析.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读