Search code examples
c++templatesfriend

friend function of a templated class needs access to member type


I need to define a friend function for the templated class. The function has return type that is a member type of the class. Now, I can not declare it beforehand, since the the return type is not known at the time. Something like this

template<class T> class A; 

//This doesn't work: error: need ‘typename’ before...  
template<class T> A<T>::member_type fcn(A<T>::member_type);

//This doesn't work: error: template declaration of ‘typename...
template<class T> typename A<T>::member_type fcn(A<T>::member_type);

template<class T>
class A{
public:
  typedef int member_type;
  friend member_type fcn<T>(member_type);
};

How do I do this?


Solution

  • I managed to compile that code on g++ using :

    template<class T> typename A<T>::member_type fcn(typename A<T>::member_type);
    

    (Thus a second 'typename' was required)