1

Is there a way to export a friend operator defined in a template class?

matrix.hpp

template<typename T>
class Matrix
{
private:
    // Some members...

public:
    // Some functions...
    
    Matrix operator+(const T&) const;
    friend Matrix operator+(const T&, const Matrix&);
};

matrix.cpp

template<typename T>
Matrix<T> Matrix<T>::operator+(const T& rhs) const
{
    // Addition of a matrix with object T...
}

template<typename T>
Matrix<T> operator+(const T& lhs, const Matrix<T>& rhs)
{
    return rhs + lhs; // Defined above
}

template __declspec(dllexport) Matrix<int> Matrix<int>::operator+(const int&) const; // This WORKS

template __declspec(dllexport) Matrix<int> operator+(const int& lhs, const Matrix<int>& rhs); // This DOESN'T COMPILE
// Compiler shows error E0801: "operator+" is not a class or function template name in the current scope

So in my executable I can do a Matrix<int> a = matrix + 10; but not Matrix<int> a = 10 + matrix;, bit frustrating...

loyd.f
  • 163
  • 1
  • 11

0 Answers0