The problem is that a function that is a friend of the class is not a function template, and the function that you actually defined is a function template.
All you have to do is make the function template for your friend as follows:
template<typename T> class enabled { private: T type_; template<typename U>
Now this compiles just fine: http://www.ideone.com/VJnck
But it makes all instances of typeof_<U> friend of all instances of enabled<T> , which means that typeof_<int> is a friend of enabled<T> for all possible value of T and vice versa.
Thus, the best solution is to force the non-template function and define it inside the class as follows:
template<typename T> class enabled { private: T type_; friend const T& typeof_(const enabled<T>& obj) { return obj.type_; } };
Demo: http://www.ideone.com/Rd7Yk
Please note that I replaced typeof with typeof_ , since GCC has an extension named typeof , and therefore it throws an error on ideone (since I cannot disable extensions).
Nawaz source share