Search code examples
c++c++11templatesdeclarationfriend

Template friend function of template class


I am wondering how to make a function friend of a class and define the function outside class if the function's template arguments include but are not limited to the class's template arguments.

For example, I have the following template class and template friend function:

template<int N>  class Matrix;
template<typename T, int N> Matrix<N> operator*(const Matrix<N> &m1, const T &m2);

// class definition
template<int N>
class Matrix{
  template<typename T>
  friend Matrix<N> operator* (const Matrix<N> &m1, const T &m2);
};

// friend function definition
template<typename T, int N> Matrix<N> operator*(const Matrix<N> &m1, const T &m2)
{
    return m1; // just as an example
}

If I compile:

Matrix<3> m;
m * 1.0;

I would get the following linker error:

test.cc:(.text+0x1c7): undefined reference to `Matrix<3> operator*<double>(Matrix<3> const&, double const&)'
collect2: error: ld returned 1 exit status

Solution

  • You have a mismatch in kind.

    Your initial declaration, and later definition, have this signature:

    template<typename T, int N>
    Matrix<N> operator*(const Matrix<N> &m1, const T &m2);
    

    This is a function template taking two template parameters: T and N.

    However, within your class, you make as a friend a function template that has this signature:

    template<typename T>
    friend Matrix<N> operator* (const Matrix<N> &m1, const T &m2);
    

    This only has one template parameter: T. N is fixed here. This friend declaration also declares this function template. This is a better match, but not actually defined, hence the behavior you see.


    You have two options I think.

    1. Remove the namespace-scope declaration of operator* and just declare and define the friended operator* within the definition of Matrix.

    2. Change the friend declaration to match the namespace-scope declaration:

      template<typename T, int M>
      friend Matrix<M> operator* (const Matrix<M> &m1, const T &m2);
      

    (1) is the better option typically - doesn't involve adding more operator*s into global scope, which is good for compile times.