friend function in template definition

送分小仙女□ 提交于 2019-12-24 05:33:15

问题


My question ist related a bit to this one.

I want to overload the operator << for some class and I found two different notations that both work:

template <class T>
class A{
  T t;
public:
  A(T init) : t(init){}
  friend ostream& operator<< <> (ostream &os, const A<T> &a); //need forward declaration
  //template <class U> friend ostream& operator<< (ostream &os, const A<U> &a);
};

Do I define identical things with different notations? Or is the first version more restrictive in which instance (in this case only the instance with the same T as my class A) of << is friend of A?


回答1:


The first version restricts the friendship to the operator<< for the specific type A<T> , while the second makes any operator<< that takes an A<SomeType> a friend.

So yes, the first one is more restrictive:

template<class T>
ostream& operator<< (ostream& os, const A<T>& a) {
    A<double> b(0.0);
    b.t; // compile error with version 1, fine with version 2
    return os;
}

int main() {
    A<int> a(0);
    cout << a << endl;
}



回答2:


It so happens that the definition of friend functions have an exception for templates. It allows you to write this:

template <class T>
class A{
  T t;
public:
  A(T init) : t(init){}
  friend ostream& operator<<(ostream &os, const A &a)
  {  // Implementation in the class
  }
};

And it has the advantage of creating a normal function automatically created for each instance of A<T> you create.

For reference: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16



来源:https://stackoverflow.com/questions/2076874/friend-function-in-template-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!