Separating template friend function's declaration and definition

56 Views Asked by At

I write a class template

template<typename T, std::size_t N, std::size_t M>
class FixedMatrix {
    /* ... */
    template<std::size_t P>
    friend auto dot (const FixedMatrix<T, N, M> &matrix1, const FixedMatrix<T, M, P> &matrix2) -> FixedMatrix<T, N, P>;
};

in FixedMatrix.h and write the definition of dot

template<typename T, std::size_t N, std::size_t M, std::size_t P>
auto dot(const FixedMatrix<T, N, M> &matrix1, const FixedMatrix<T, M, P> &matrix2) -> FixedMatrix<T, N, P> {
    /* ... */
}
template class FixedMatrix<std::complex<double>, 2, 2>;

in FixedMatrix.cpp.

Reading https://isocpp.org/wiki/faq/templates#template-friends, I add

template<typename T, std::size_t N, std::size_t M>
class FixedMatrix;

template<typename T, std::size_t N, std::size_t M, std::size_t P>
auto dot(const FixedMatrix<T, N, M> &matrix1, const FixedMatrix<T, M, P> &matrix2) -> FixedMatrix<T, N, P>;

above the definition of class FixedMatrix. However, the compiler still cannot find reference to dot(). If I modify the declaration of the friend function as

template<std::size_t P>
friend auto dot <> (const FixedMatrix<T, N, M> &matrix1, const FixedMatrix<T, M, P> &matrix2) -> FixedMatrix<T, N, P>;

the compiler will complain that "function template partial specialization is not allowed" and "invalid use of template-id 'dot<>' in declaration of primary template".

Is there any way to separate template friend function's declaration and definition?

0

There are 0 best solutions below