繁体   English   中英

模板中的重载解析

[英]overload resolution in templates

任何人都可以举例说明

  "Involving conversions on a function argumentsinvolved in template
   parameter deduction."

像这样的示例:

 template<class T> struct B { /* ... */ };
 template<class T> struct D : public B<T> { /* ... */ };
 template<class T> void f(B<T>&);
 void g(B<int>& bi, D<int>& di)
   {
    f(bi);
    f(di);
   }

请给我更多例子

编辑:这是从ISO C ++标准14.8.3 / 5th点/语句:重载分辨率

它与示例显示的内容有关。 总而言之,这些就是转化

  • 函数参数可以是Base<T> ,而函数参数是Derived<T> ifstream << "hello"进行比较-以这种方式推导出operator<<左侧。
  • 函数参数可以是const U& ,而函数参数可以是U (与volatile相同)。
  • 函数参数可以是const U*const EC::*而函数参数分别是U*EC::* (这些是资格转换)-与volatile相同。

无法对参与推导的函数参数/自变量进行其他转换。 转换的整个范围可如果一个函数的参数参与扣除被应用,但是这需要一个非推断上下文或者没有在所有推导论证背景下,和实施实际上并不同意它(看到这里 )。

换一种说法:

template<typename T> struct id { typedef T type; };
template<typename T> void f(T, typename id<T>::type);

int main() {
  // deduction acts on void(int*, int*) - deduction does not need
  // to deduce anything and follows [temp.arg.explicit]p4
  f<int*>(0, 0); 

  // deduction acts on void(T, id<T>::type) - second is a non-deduced context,
  // which *will* allow the conversion of int -> int*, since it will not compare
  // the argument with parameter during deduction (since it is non-deduced).
  f((int*)0, 0);
}

第二个示例对于某些局部排序上下文的工作至关重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM