非模板友元 template<typename T> class HasFriend { public: friend void report(HasFriend<T>&); }; // the following code is necessary void report(HasFriend<int>& hf) { //details } void report(HasFriend<double>& hf) { //details } ====================================================================== 约束模板友元函数 即友元的类型取决于类被实例化时的类型(TT) template<typename T> void report(T&); template <typename TT> class HasFriendT { public: friend void report<>(HasFriend<TT>&); //参数推断 //or friend void report< HasFriend<TT> >(HasFriend<TT>&); } template<typename T> void report(T& hf) { //details } ====================================================================== 非约束模板函数 //在类外照样用,常用这个 template<typename T> class ManyFriend { public: template<typename C,typename D> friend void show(C&,D&); }; template<typename C,typename D> void show(C& c,D& d) { //details } //类型推断在起作用
时间: 2024-10-04 06:48:50