如何将模板定义和实例化分为HPP和IPP文件

How do I split template defintion and instantiation into hpp and ipp files

本文关键字:HPP IPP 文件 实例化 定义      更新时间:2023-10-16

我想将模板类分为两个文件,例如正常类如何在声明所在的.hpp和.hpp and a的.ipp中实现。

我已经使用了普通方法。但是,使用本身就是模板的方法,我正面临一些问题。

使用以下结构:

#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;
}

现在,当我对此进行编译时,它会在以下消息中失败:

../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;

我不确定如何处理此问题,因为编译器无法识别它。有办法解决此问题吗?

我希望有人可以帮助我。谢谢!

您需要定义两组模板参数:一个用于矩阵类模板,另一个用于矩阵类功能,而不是将它们合并到一个长列表中:

template<typename t, std::size_t m, std::size_t n>
template<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
{

还要注意,您的包含实际上是混乱的。包括" matrix.hpp"进入.ipp文件不起作用,因为您已经在" matrix.hpp"中包含了" matrix.ipp"。尽管这不会导致错误。