Assume we have a template class named Matrix, and we want to add a friend function called swap().
Method 1
1 | template<typename T> |
Notice, the swap() is not a template function! It’s just a normal function.
Why we need to define it inside the class?
Because when we create a
Matrix<int>, it will also define avoid swap(Matrix<int>&, Matrix<int>&)for system; forMatrix<double>, it’s the same. In other word, when we create certain type Matrix, the swap which supports that certain type will be automatically created for us. So that when we useswap(), it always make sure we can find the definition.
Can we define it outside the class?
Yes, you can, but you need to make sure you write definitions for all possible types, like you need to implement a
void swap(Matrix<int>&, Matrix<int>), avoid swap(Matrix<double>&, Matrix<double>), etc for all possibletypename T.
Method 2
1 | template<typename T> |
Notice here the swap is a template function, so that we need to write it as swap<>, and we don’t need to explicitly denote Matrix type, cuz template funtion can deduce its type.
Can we don’t write the declarition of template swap?
No. Inside class definition, we tell the class that we have a template friend function. If we don’t declare it before class definition, the class don’t know how many typenames it has, do it have default typename, etc.
Trick
1 | template<typename T> void helper(T); |