문제

I have a Matrix class with a friend function to use with operator<<. This all works fine but I now want to partially specialize that friend function to work differently if the Matrix class has Matrix as its template parameter (i.e. when the instance of the class has been declared like Matrix< Matrix< char > >). In the class definition first I had

template <typename U>
friend std::ostream& operator<<(std::ostream& output, const Matrix<U>& other);

and I tried adding

friend std::ostream& operator<<(std::ostream& output, const Matrix<Matrix<char> >& other);

but this gave me multiple declaration errors from the compiler. I can't seem to figure out how to accomplish this.

도움이 되었습니까?

해결책

There's no such thing as a partial specialization of a function template.

You need overloading, not specialization. This should compile, link, and run cleanly (it does for me):

#include <iostream>

template <typename T>
class Matrix {
  public:
    template <typename U> friend std::ostream& 
        operator<<(std::ostream& output, const Matrix<U>& other);
    friend std::ostream& 
        operator<<(std::ostream& output, const Matrix<Matrix<char> >& other);    
};


template <typename U>
std::ostream& 
operator<<(std::ostream& output, const Matrix<U>& other)
{
    output << "generic\n";
    return output;
}

std::ostream& 
operator<<(std::ostream& output, const Matrix<Matrix<char> >& other)
{
    output << "overloaded\n";
    return output;
}

int main ()
{
    Matrix<int> a;
    std::cout << a;

    Matrix<Matrix<char> > b;
    std::cout << b;
}

If you are getting compiler errors from this, you probably have a buggy compiler.

다른 팁

Try writing the specialization explicitly:

template <>
friend std::ostream& operator<< <Matrix<char> >(std::ostream& output,
                                       const Matrix<Matrix<char> >& other);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top