0

I am writing simple classes that implement vectors and matrices as part of trying to learn OpenGL. I have matrix and vector classes that look like this :

// Forward declarations
template <typename T, size_t N/*len*/> struct vec;
template<typename T, size_t N /*rows*/, size_t M /*cols*/> struct mat;

// Forward declare *operator for matrix
// (NxM) matrix multiplied by (MxP) matrix yields (NxP) matrix
mat<T, N, P> operator* (const mat<T, N, M>& A, const mat<T, M, P>& B);

template <typename T, size_t N>
struct vec {
    public:
        vec() {}
        virtual ~vec() {}
    private:
        T[N] m_data;
};

template <typename T, size_t N, size_t M>
struct mat {
    public:
        mat() {}
        virtual ~mat() {}
        // This is where it gets interesting. By my reading of the rules
        // of C++11, this counts as a partial specialization of the 
        // operator template, and should not work. 
        // However, it compiles just fine!
        template <size_t n, size_t m, size_t p> 
        friend mat<T, n, p> operator* (const mat<T, n, m>& A,
                                         const mat<T, m, p> &B); 
        // Implementation appears later in the same header file. 
    private:
        T[N*M] m_data;
};

I declare the * operator as a friend because I want it to have access to the internal m_data member, but I don't want the users of 'mat' and 'vec' to know the internals.

This compiles and runs just fine. I have a unit test for that matrix multiplication, and it works just fine. However, I don't know why it even compiles, let alone runs. By my reading of the rules of C++ templates, the declaration of the * operator counts as a partial specialization of function template, and is illegal.

What am I missing here?

Salil Deosthale
  • 41
  • 1
  • 1
  • 2

1 Answers1

0

Turns out this does *NOT* compile. I thought it was compiling because I wasn't invoking the template stream operator in my unit test when I thought I was.

Sorry for the stupid question!

Salil Deosthale
  • 41
  • 1
  • 1
  • 2