Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重构类:无法将派生类中成员函数的公共代码移回基类 我试图根据CH29“C++编程语言的一个矩阵设计”来实现C++矩阵库()。并且发现我无法将矩阵和矩阵_ref中完全相同的成员函数移回矩阵_base_C++_C++11_Templates_Matrix_Refactoring - Fatal编程技术网

重构类:无法将派生类中成员函数的公共代码移回基类 我试图根据CH29“C++编程语言的一个矩阵设计”来实现C++矩阵库()。并且发现我无法将矩阵和矩阵_ref中完全相同的成员函数移回矩阵_base

重构类:无法将派生类中成员函数的公共代码移回基类 我试图根据CH29“C++编程语言的一个矩阵设计”来实现C++矩阵库()。并且发现我无法将矩阵和矩阵_ref中完全相同的成员函数移回矩阵_base,c++,c++11,templates,matrix,refactoring,C++,C++11,Templates,Matrix,Refactoring,基类是纯抽象类(): 其中matrix\u impl::requising\u element()确保Args…中的整数可以转换为std::size\t: template<bool B, typename T = void> using Enable_if = typename std::enable_if<B, T>::type; template<typename X, typename Y> constexpr bool Convertible()

基类是纯抽象类():

其中
matrix\u impl::requising\u element()
确保
Args…
中的整数可以转换为
std::size\t

template<bool B, typename T = void>
using Enable_if = typename std::enable_if<B, T>::type;

template<typename X, typename Y>
constexpr bool Convertible() { return std::is_convertible<X, Y>::value; }

constexpr bool All() { return true; }

template<typename... Args>
constexpr bool All(bool b, Args... args) { return b && All(args...); }

template<typename... Args>
constexpr bool Requesting_element() {
  return All(Convertible<Args, size_t>()...);
}

如果要编译该项目,可能需要同时安装cmake和intel MKL(我想实现一些模板函数作为CBLAS的接口)。我认为这是简单的类重构,但它只需要几个小时,我不知道为什么会发生。

我找到了解决方案,这似乎是一个重复的问题( )

现在,代码已移动到
矩阵库

template<typename T, std::size_t N>
template<typename... Args>
T &MatrixBase<T, N>::operator()(Args... args) {
  assert(matrix_impl::check_bounds(this->desc_, args...));
  return *(data() + this->desc_(args...));
}

template<typename T, std::size_t N>
template<typename... Args>
const T &MatrixBase<T, N>::operator()(Args... args) const {
  assert(matrix_impl::check_bounds(this->desc_, args...));
  return *(data() + this->desc_(args...));
}
请注意,脚本检查将只在派生类中进行,因为基类是纯抽象类,并且我们永远不会创建
MatrixBase
的对象

template<typename T, size_t N>
class Matrix_ref : public Matrix_base<T,N> {
    // special to Matrix_ref
  public:
    T *data() { return ptr_; }  // "flat" element access
    const T *data() const { return ptr_; }
  private:
    T* ptr;  // the first element in the Matrix
};
// m(i,j,k) subscripting with integers
template<typename T, std::size_t N>
template<typename... Args>
Enable_if<matrix_impl::Requesting_element<Args...>(), T &>
Matrix<T, N>::operator()(Args... args) {
  assert(matrix_impl::check_bounds(this->desc_, args...));
  return *(data() + this->desc_(args...));
}

template<typename T, std::size_t N>
template<typename... Args>
Enable_if<matrix_impl::Requesting_element<Args...>(), const T &>
Matrix<T, N>::operator()(Args... args) const {
  assert(matrix_impl::check_bounds(this->desc_, args...));
  return *(data() + this->desc_(args...));
}
template<bool B, typename T = void>
using Enable_if = typename std::enable_if<B, T>::type;

template<typename X, typename Y>
constexpr bool Convertible() { return std::is_convertible<X, Y>::value; }

constexpr bool All() { return true; }

template<typename... Args>
constexpr bool All(bool b, Args... args) { return b && All(args...); }

template<typename... Args>
constexpr bool Requesting_element() {
  return All(Convertible<Args, size_t>()...);
}
/Users/pany/statslabs-project/matrix/examples/dgemm_example.cpp:33:7: error: no matching function for call to object of type 'slab::mat' (aka 'Matrix<double, 2>')
      A(i, j) = (double) (i * k + j + 1);
      ^
/Users/pany/statslabs-project/matrix/include/slab/matrix/traits.h:24:43: note: candidate template ignored: disabled by 'enable_if' [with Args = <int, int>]
using Enable_if = typename std::enable_if<B, T>::type;
                                      ^
/Users/pany/statslabs-project/matrix/include/slab/matrix/traits.h:24:43: note: candidate template ignored: disabled by 'enable_if' [with Args = <int, int>]
template<typename T, std::size_t N>
template<typename... Args>
T &MatrixBase<T, N>::operator()(Args... args) {
  assert(matrix_impl::check_bounds(this->desc_, args...));
  return *(data() + this->desc_(args...));
}

template<typename T, std::size_t N>
template<typename... Args>
const T &MatrixBase<T, N>::operator()(Args... args) const {
  assert(matrix_impl::check_bounds(this->desc_, args...));
  return *(data() + this->desc_(args...));
}
// m(i,j,k) subscripting with integers
template<typename... Args>
Enable_if<matrix_impl::Requesting_element<Args...>(), T &>
operator()(Args... args) {
  return MatrixBase<T,N>::template operator()<Args...>(args...);
}

template<typename... Args>
Enable_if<matrix_impl::Requesting_element<Args...>(), const T &>
operator()(Args... args) const {
  return MatrixBase<T,N>::template operator()<Args...>(args...);
}