gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:04:16 24 4
gpt4 key购买 nike

我正在尝试制作一个固定大小的矩阵类。目的是让它继承或利用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com