I was learning about non-template friend function & template friend function to a templated class. So I tried the code below :
#include <iostream>
template<typename T>
class cl
{
private :
T val;
public:
cl()= default;
explicit cl(T v) : val(std::move(v)) {}
friend void non_template_friend(cl m);
};
template <typename T>
void non_template_friend(cl<T> m) { std::cout << m.val << std::endl;}
int main()
{
cl<int> c(10);
non_template_friend(c);
return 0;
}
so when I compile I got : undefined reference to
non_template_friend(cl)' ` So to resolve that I have to move the friend function definition inside the class definition like so :
template<typename T>
class cl
{
private :
T val;
public:
cl()= default;
explicit cl(T v) : val(std::move(v)) {}
friend void non_template_friend(cl m) { std::cout << m.val << std::endl;}
};
But I was wondering,is there any trick to do to be able to define the friend fuinction outside the class definition ?
Thank you.