I want to split my template classes into 2 files like how normal classes function with a .hpp where the declaration is and an .ipp where the implementation lives.
I have got this working with normal methods. But with methods that are themselves templates I am facing some issues.
Using the following structure:
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include <array>
#include <type_traits>
template<typename t,
std::size_t m,
std::size_t n>
class Matrix
{
static_assert(std::is_arithmetic<t>::value,
"Matrix can only be declared with a type where std::is_arithmetic is true.");
public:
Matrix();
template<std::size_t y, std::size_t x, std::size_t p, std::size_t q>
Matrix<t, p, q> slice() const;
private:
std::array<std::array<t, n>, m> data{};
};
#include "Matrix.ipp"
#endif
Matrix.ipp:
#include "Matrix.hpp"
template<typename t, std::size_t m, std::size_t n>
Matrix<t, m, n>::Matrix()
{}
template<typename t,
std::size_t m,
std::size_t n,
std::size_t y,
std::size_t x,
std::size_t p,
std::size_t q>
Matrix<t, p, q> Matrix<t, m, n>::slice() const
{
auto mat = Matrix<t, p, q>();
for (std::size_t i = y; i < m; i++)
{
for (std::size_t j = x; j < n; j++)
{
mat[i - y][j - x] = (*this)[i][j];
}
}
return mat;
}
main.cpp
#include "Matrix.hpp"
int main() {
auto m = Matrix<3, 3, int>();
auto sliced = m.template slice<1, 1, 2, 2>();
return 0;
}
Now when I compile this it fails with the following message:
../Matrix.ipp:14:17: error: prototype for ‘Matrix<t, p, q> Matrix<t, m, n>::slice() const’ does not match any in class ‘Matrix<t, m, n>’
Matrix<t, p, q> Matrix<t, m, n>::slice() const
^~~~~~~~~~~~~~~
In file included from ../main.cpp:0:0:
../Matrix.hpp:20:18: error: candidate is: template<class t, long unsigned int m, long unsigned int n> template<long unsigned int y, long unsigned int x, long unsigned int p, long unsigned int q> Matrix<t, p, q> Matrix<t, m, n>::slice() const
Matrix<t, p, q> slice() const;
I am not sure how to handle this since the compiler won't recognize it. Is there a way to fix this?
I hope somebody can help me. Thanks!