小弟我看<<C++ primer>>,看到了16.4(P682)讲的是类模板的友元声明
2.绑定的友元类模板或函数模板
说一个友元(也是模板实例)如果要和类模板形成一对一的映射就应该如下定义
template<class type>
class testclass {
friend void fun<type>(testclass<type> &a);
...
};
template<class type>
void fun(testclass<type>);
可是书后面接着讲了一个例子,是重载operator<<的,也是声明成了绑定的友元函数模板可是只写成
template<class type>
class testclass {
friend ostream &operator<<(ostream &, const testclss<type> &);
...
}
我觉的不妥,于是在DEV-C++下写了一个测试,果然编不过去,于是改成
friend ostream &operator<<<type>(ostream &, const testclss<type> &);
编译通过,运行正常...
不知道那个定义是正确,望高人指教
C++ Primer有一些疏漏,如果该 friend function 在绑定 friend function to template class 的时候,该函数参数含有 template 参数的话,则需要 <> 或者<type>绑定,否则就无所谓。
up
你正确
可能情况一楼也说了
up
人无完人,何况书乎?
书是人写滴,人无完人,那么 traits 出来,书无完书。
你用什么编辑器