Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Try writing the specialization explicitly:

template <>
friend std::ostream& operator<< <Matrix<char> >(std::ostream& output,
                                       const Matrix<Matrix<char> >& other);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top