CRTP和表达式模板线性代数

时间:2015-05-21 23:09:55

标签: c++ templates c++11 expression crtp

我试图修改我的线性代数模块以避免虚拟vtable事情。试图使用CRTP和表达式模板。 我选择了一些基本的东西来测试整个事情,但我无法让它发挥作用。

我有4节课,说: 基础CRTP类,这里是Mathbase

template <typename Derived>
class Mathbase
{
 public:
 using T = typename dense_traits<Derived>::T;

  Derived& derived() { return static_cast<Derived&>(*this); }
  const Derived& derived() const { return static_cast<const Derived&>(*this); }

  T& coeff(std::size_t row, std::size_t col) { return derived().coeff(row, col); }
  const T& coeff(std::size_t row, std::size_t col) const { return derived().coeff(row, col); }
  T& coeff(std::size_t index) { return derived().coeff(index); }
  const T& coeff(std::size_t index) const { return derived().coeff(index); }
};

然后,我在Densebase中实现了transpose,determinant等函数:

template <typename Derived>
class Densebase : public Mathbase<Derived>
{
 public:

 using Submat = Subview<Derived, dense_traits<Derived>::M-1, dense_traits<Derived>::N-1>;
 using ConstSubmat = const Subview<const Derived, dense_traits<Derived>::M-1, dense_traits<Derived>::N-1>;

  Submat sub(std::size_t row, std::size_t col) { return Submat(derived(), row, col); }
  ConstSubmat sub(std::size_t row, std::size_t col) const { return ConstSubmat(derived(), row, col); }
};

请注意,它声明了两种类型,它们对父矩阵进行引用(co-matrix) 然后我有Matrix类,通过访问其存储来实现coeff函数:

template <typename T, std::size_t M, std::size_t N>
class Matrix : public Densebase<Matrix<T,M,N>>
{
  // nothing fancy
};

现在,有两件事情不起作用:

  • 我实现了一个使用拉普拉斯扩展的行列式助手(计算共矩阵行列式并对它们求和,遗憾的是它无法编译

    template <typename Derived, std::size_t N>
    struct det_helper
    {
       static inline typename dense_traits<Derived>::T run(const Densebase<Derived>& dense)
       {
         typename dense_traits<Derived>::T det = 0;
         // Laplace expansion
         for (std::size_t i = 0; i < N; ++i)
           det += ((i & 1) ? -1 : 1)*dense.coeff(0,i)*det_helper::run(dense.sub(0,i));
    
         return det;
       }
     };
    
     template <typename Derived>
     struct det_helper<Derived, 2>
     {
       static inline typename dense_traits<Derived>::T run(const Densebase<Derived>& dense)
       {
         return dense.coeff(0,0)*dense.coeff(1,1) - dense.coeff(0,1)*dense.coeff(1,0);
       }
     };
    

并且这样称呼:

T determinant() const
{
  return det_helper<Derived, dense_traits<Derived>::M>::run(derived());
}
  • 第二个问题是我在<< stream operator上实现了一个过载,它在Matrix上工作正常但在stream << matrix.sub(0,0);上崩溃,甚至没有输入该函数。

以下是PASTEBIN

上传的完整代码

并按要求输出错误:

main.cpp: In instantiation of 'static typename dense_traits<T>::T det_helper<Derived, N>::run(const Densebase<Derived>&) [with Derived = Matrix<float, 3ul, 3ul>; long unsigned int N = 3ul; typename dense_traits<T>::T = float]':
main.cpp:68:62:   required from 'typename Densebase<Derived>::base::T Densebase<Derived>::determinant() const [with Derived = Matrix<float, 3ul, 3ul>; typename Densebase<Derived>::base::T = float]'
main.cpp:159:30:   required from here
main.cpp:33:65: error: no matching function for call to 'det_helper<Matrix<float, 3ul, 3ul>, 3ul>::run(Densebase<Matrix<float, 3ul, 3ul> >::ConstSubmat)'
       det += ((i & 1) ? -1 : 1)*dense.coeff(0,i)*det_helper::run(dense.sub(0,i));
                                                                 ^
main.cpp:28:51: note: candidate: static typename dense_traits<T>::T det_helper<Derived, N>::run(const Densebase<Derived>&) [with Derived = Matrix<float, 3ul, 3ul>; long unsigned int N = 3ul; typename dense_traits<T>::T = float]
   static inline typename dense_traits<Derived>::T run(const Densebase<Derived>& dense)
                                                   ^
main.cpp:28:51: note:   no known conversion for argument 1 from 'Densebase<Matrix<float, 3ul, 3ul> >::ConstSubmat {aka const Subview<const Matrix<float, 3ul, 3ul>, 2ul, 2ul>}' to 'const Densebase<Matrix<float, 3ul, 3ul> >&'

1 个答案:

答案 0 :(得分:1)

我不知道你的编译错误是什么,但Mathbase::coeffDensebase::sub的重载具有相同的签名(名称和参数类型),这些签名将无法编译。你不能仅仅通过它的返回类型来重载函数,它看起来就像你想要做的那样。