[英]function template deduction using raw pointer as argument
案例A:
template<class T>
class A{
public:
A(T*)
{
cout<<"T*"<<endl;
}
template<class Y>
A(Y*)
{
cout<<"Y*"<<endl;
}
};
然后致电:
A<int>p1(new int); //T*
A<int>p2(new double); //Y*
我能理解这种情况。
案例B:
template<class T>
class B{
public:
template<class Y>
B(Y*)
{
cout<<"Y*"<<endl;
}
};
然后致电:
B<int>p3(new int); //Y*
B<int>p4(new double); //Y*
题:
我不明白情况B并弄清楚为什么B<int>p3(new int);
叫B(Y*)
吗?有人知道为什么吗?
使用模板构造函数时,类型参数Y
类型推导不依赖于您使用B<int>
明确指定的类型参数T
这意味着在以下情况下:
B<int>p3(new int)
从类型int*
的参数推导得出Y
为int
。
同样,在以下情况下:
B<int>p4(new double)
从double*
类型的参数推导得出Y
为double
。
类模板类型参数T
和构造函数模板类型参数Y
之间没有连接-它们的推论是分别执行的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.