gpt4 book ai didi

c++ - 模板中的重载决议

转载 作者:行者123 更新时间:2023-11-30 03:09:34 24 4
gpt4 key购买 nike

请举例说明

  "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 E C::*而函数参数是 U*E C::*分别(这些是资格转换) - volatile 相同。

不能对参与推导的函数参数/参数进行其他转换。如果函数参数参与推导,则可以应用整个转换范围,但这需要非推导上下文或根本没有要推导的参数的上下文,并且实现不'实际上同意它(参见 here )。

换句话说:

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);
}

第二个例子对于 some partial ordering 至关重要工作环境。

关于c++ - 模板中的重载决议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4013922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com