【问题标题】:C++ Overload Resolution, userdefined conversion and function templateC++ 重载解析、用户定义转换和函数模板
【发布时间】:2016-07-19 06:40:50
【问题描述】:

使用 g++ 3.4 和 4.7 我观察到以下奇怪的行为:

如果需要用户定义的转换,则函数模板不匹配,而普通函数则需要。 我在 C++98 标准中找不到相应的规则。 g++ 是否正确(如我所料)?还是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;
}

【问题讨论】:

    标签: c++ function templates implicit-conversion overload-resolution


    【解决方案1】:

    GCC 是正确的,template argument deduction 不考虑隐式转换。

    类型推导不考虑隐式转换(除了上面列出的类型调整):这是重载解析的工作,稍后会发生。

    对于您的代码,auto_ptr_refauto_ptr 不匹配,模板参数T 的推演失败,因此函数模板x() 根本不会考虑进行重载解析。

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 2012-09-29
      相关资源
      最近更新 更多