What to do with friend functions of a class under template?
Printable View
What to do with friend functions of a class under template?
easiest way is to define the friend function within the template
to use the canonical example of op <<
of course, this isn't really cool if you want to use the same friend function for multiple templates.Code:
template <typename T>
class SomeTemplate
{
private:
T m_t;
public:
// some methods
friend std::ostream &operator>>(std::ostream &os, const SomeTemplate<T> &inst)
{
os >> inst.m_t;
return os;
}
};
What's the difference between a friend function declared within the class and one declared outside the class?