pair种中的模板构造函数
pair类中的模板构造函数
在pair类里的模板构造函数
template <class T1,class T2>
{
..
template <class u,class v>
pair(const pair<u,v> &p):first(p.first),second(p.second){}
...
}
书上说是因为构造过程中可能需要隐式转换,调用有系统生成的copy构造函数,
void f(std::pair<int,const char*>)
void g(std::pair<const int,std::string>)
void foo()
{
std::pair<int,const char*> p(42,"hello");
f(p);
g(p);
}
但是我用
pair(const pair<T1,T2>& p):first(p.first),second(p.second){}也能实现啊,为什么非得用成员模板函数呢
------解决方案--------------------
在pair类里的模板构造函数
template <class T1,class T2>
{
..
template <class u,class v>
pair(const pair<u,v> &p):first(p.first),second(p.second){}
...
}
书上说是因为构造过程中可能需要隐式转换,调用有系统生成的copy构造函数,
void f(std::pair<int,const char*>)
void g(std::pair<const int,std::string>)
void foo()
{
std::pair<int,const char*> p(42,"hello");
f(p);
g(p);
}
但是我用
pair(const pair<T1,T2>& p):first(p.first),second(p.second){}也能实现啊,为什么非得用成员模板函数呢
------解决方案--------------------
- C/C++ code
参考: http://dev.yesky.com/441/2256441.shtml