const参考和虚拟模板继承

const reference and virtual template inheritance

本文关键字:继承 虚拟 参考 const      更新时间:2023-10-16

我在这里发布,因为我不确定为什么会遇到这个问题。.加上我不知道该如何解决。

我的目标是构建具有矩阵的线性代数系统,并能够在行或COLS或子膜上检索参考。

我有一个纯粹的虚拟类,它可以定义函数并保持对元素虚拟的访问:

template <typename T, std::size_t M, std::size_t N>
class densebase
{
public:
  using SubMat = submat<T,M-1,N-1,densebase>;
  using ConstSubMat = const submat<T,M-1,N-1,const densebase>;
  virtual T& coeff(std::size_t row, std::size_t col) = 0;
  virtual T& coeff(std::size_t index) = 0;
  virtual const T& coeff(std::size_t row, std::size_t col) const = 0;
  virtual const T& coeff(std::size_t index) const = 0;
  SubMat sub(std::size_t row, std::size_t col) { return SubMat(*this, row, col); }
  ConstSubMat sub(std::size_t row, std::size_t col) const { return ConstSubMat(*this, row, col); }
};

然后,我有矩阵类,它处理数据并覆盖COEFF函数

template <typename T, std::size_t M, std::size_t N>
class mat : public densebase<T,M,N>
{
public:
  // constructors..
  T& coeff(std::size_t row, std::size_t col) { return data_[row*N + col]; }
  T& coeff(std::size_t index) { return data_[index]; }
  const T& coeff(std::size_t row, std::size_t col) const { return data_[row*N + col]; }
  const T& coeff(std::size_t index) const { return data_[index]; }
private:
  std::vector<T> data_;
};

最后,subpatrix类,该类将densebase上的引用作为参数,并以不同的方式覆盖coeff函数:

template <typename T, std::size_t M, std::size_t N, typename parent>
class submat : public densebase<T,M,N>
{
public:
  submat(parent& m, std::size_t row, std::size_t col)
  : ref_(m), row_(row), col_(col)
  {
  }
  T& coeff(std::size_t row, std::size_t col) { return ref_(row + (row >= row_), col + (col >= col_)); }
  T& coeff(std::size_t index) { return ref_(index + (index >= row_),
                                            index + (index >= col_)); }
  const T& coeff(std::size_t row, std::size_t col) const { return ref_(row + (row >= row_), col + (col >= col_)); }
  const T& coeff(std::size_t index) const { return ref_(index + (index >= row_),
                                                        index + (index >= col_)); }
private:
  parent& ref_;
  std::size_t row_, col_;
};

当我尝试从常数矩阵检索子序列时,这里出现了问题,我得到了汇编错误" float&amp;'的引用的无效初始化"从类型" const float'的表达" 。我虽然COEFF总是会返回一个参考,因为它被定义了。

完整错误消息:

||=== Build: Debug in ImDev (compiler: GNU GCC Compiler) ===|
C:ImagineImaginedemosImDevmain.cc||In instantiation of 'T& submat<T, M, N, parent>::coeff(std::size_t, std::size_t) [with T = float; unsigned int M = 2u; unsigned int N = 2u; parent = const densebase<float, 3u, 3u>; std::size_t = unsigned int]':|
C:ImagineImaginedemosImDevmain.cc|206|required from here|
C:ImagineImaginedemosImDevmain.cc|90|error: invalid initialization of reference of type 'float&' from expression of type 'const float'|
C:ImagineImaginedemosImDevmain.cc||In instantiation of 'T& submat<T, M, N, parent>::coeff(std::size_t) [with T = float; unsigned int M = 2u; unsigned int N = 2u; parent = const densebase<float, 3u, 3u>; std::size_t = unsigned int]':|
C:ImagineImaginedemosImDevmain.cc|206|required from here|
C:ImagineImaginedemosImDevmain.cc|92|error: invalid initialization of reference of type 'float&' from expression of type 'const float'|
C:ImagineImaginedemosImDevmain.cc||In member function 'T& submat<T, M, N, parent>::coeff(std::size_t) [with T = float; unsigned int M = 2u; unsigned int N = 2u; parent = const densebase<float, 3u, 3u>; std::size_t = unsigned int]':|
C:ImagineImaginedemosImDevmain.cc|92|warning: control reaches end of non-void function [-Wreturn-type]|
C:ImagineImaginedemosImDevmain.cc||In member function 'T& submat<T, M, N, parent>::coeff(std::size_t, std::size_t) [with T = float; unsigned int M = 2u; unsigned int N = 2u; parent = const densebase<float, 3u, 3u>; std::size_t = unsigned int]':|
C:ImagineImaginedemosImDevmain.cc|90|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 2 error(s), 6 warning(s) (0 minute(s), 0 second(s)) ===|

我错过了什么?

using ConstSubMat = const submat<T, M - 1, N - 1, const densebase>;

应该是

using ConstSubMat = const submat<const T, M - 1, N - 1, const densebase>;

保留constness。