Template friend function of template class

♀尐吖头ヾ 提交于 2021-01-28 05:51:29

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/51141559/template-friend-function-of-template-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!