c++ - 二维数组的自动大小扣除

标签 c++ templates matrix c++17 stdarray

我正在尝试制作一个固定大小的矩阵类。目的是让它继承或利用 std::arraystd::array :

template <size_t Rows, size_t Columns, class T=double>
struct Matrix : public std::array<std::array<T, Columns>, Rows> {
};
我想提供一个可以自动推断大小的初始化器,比如 std::array可以在 C++17(我正在使用)中使用。我也可以使用函数来制作 Matrix而不是使用类模板参数推导。
// What I want, but does not work:
Matrix matrix {{1., 2.},
               {3., 4.}};

// Or:
auto matrix = MakeMatrix ({1., 2.},
                          {3., 4.});
我未能使其中任何一个成为可能。相反,只有以下工作:
// Requires specifying the size and repeating `std::array`, both undesired
Matrix<2,2> mat {
    std::array{1., 2.},
    std::array{3., 4.}
};

// OR this, which requires specifying the size and is generally confusing
Matrix<2,2> mat2 {1., 2., 
                  3., 4.};
我尝试使用可变参数模板,但这也没有吸引编译器:
template<class... Args>
auto MakeMatrix (Args... args) {
  return Matrix{ std::array {args} ... };
}

// This causes compiler error:
// error: no matching function for call to 'MakeMatrix'
// candidate function [with Args = <>] not viable: requires 0 arguments, but 2 were provided
auto matrix = MakeMatrix ({1., 2.},
                          {3., 4.});

// This causes compiler error
// error: no viable constructor or deduction guide for deduction of template arguments of 'Matrix'
// note: in instantiation of function template specialization 'MakeMatrix<std::__1::array<double, 2>, std::__1::array<double, 2> >'
// note: note: candidate function template not viable: requires 0 arguments, but 2 were provided
auto matrix = MakeMatrix (std::array {1., 2.},
                          std::array {3., 4.});

我也考虑过使用 std::initializer_list<std::initializer_list<T>> ,但是据我所知,这些不支持固定大小,我希望在编译时确定大小。
关于如何做到这一点的任何想法,或者使用当前的 C++ 机制是不可能的?

最佳答案

问题是编译器无法推导出{}当用作参数时。这适用于 initializer_list (对于构造函数,因为一些特殊规则)。但是,您缺少尺寸。
解决方法是内置数组:

template <typename T, size_t N>
using Row = const T (&)[N]; // for readability

template <auto Rows, auto Columns, typename T = double>
class Matrix {
public:
  template <typename... Ts, auto N>
  Matrix(Row<Ts, N>... rows) {}
};

template <typename... RowTypes, auto Columns>
Matrix(Row<RowTypes, Columns>...)
    -> Matrix<sizeof...(RowTypes), Columns, std::common_type_t<RowTypes...>>;
您现在可以构建 Matrix完全如你所愿:
const auto m = Matrix{{1, 2}, {1, 2}, {1, 2}};
最后一步,初始化 std::array使用内置数组可能会很棘手。 C++20 提供了一个 function ,检查链接以了解可能的实现。如果您复制该实现,或者有一个可用的实现,则可以轻松创建构造函数,如下所示:
template <auto Rows, auto Columns, typename T = double>
class Matrix {
public:
  template <typename... Ts, auto N>
  Matrix(Row<Ts, N>... rows) {
    data_ = {to_array(rows)...};
  }
private:
  std::array<std::array<T, Columns>, Rows> data_;
};
Live example, with operator[] to show that the data layout is correct .

关于c++ - 二维数组的自动大小扣除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63211172/

相关文章:

c++ - COM:创建 BSTR 的 SafeArray 的访问冲突

c++ - Qt - 信息未加载到 ListView 中

c++ - 存储任何(但恒定)长度的 std::arrays 集

java - 将 ArrayList 转换为二维数组

c++ - 如何在 CUDA 中执行多个矩阵乘法?

c++ - "has-a"和 "is-a"是什么意思?

c++ - 检测循环并加快求和速度

c++ - 如何存储或转发 CRTP 模板类的类型

c++ - std::min 无法将枚举常量解释为有效整数类型 (g++ 4.6.3)

r - 以行和为条件提取矩阵行?