template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
class Array final
{
private:
std::vector<size_t> shape_;
std::vector<T> data_;
public:
// Some public methods
}
T
是存储在数组中的元素类型,而数组的维度不是模板化的,因为用户应该能够重新调整数组的形状,例如:
Array<int> array = Array<int>::zeros(3, 2, 4);
array.reshape(4, 6);
虽然上述功能的实现进展顺利,但我在实现这个类的“开始”时遇到了困难,即初始化数组……我的问题如下:
Is there any method to have an constructor like this, such that nested initializer lists of different depths create different arrays, like:
Array<int> a = {1, 2}; // Dimension: 1, Shape: 2 Array<int> b = {{1}, {2}}; // Dimension: 2, Shape: 2x1
My approach to implement the constructors made these two arrays the same, which is not what I want. Plus, clang complained about the braced scalars, which seem to be the problem. Currently my naive approach which suffers from the problem above looks like this
... Array() :data_(0), shape_{0} {} Array(std::initializer_list<T> list) :data_(list), shape_{list.size()} {} Array(std::initializer_list<Array> lists) { // Implementation } ...
It's easy for the compiler to deduct the types for the following arrays:
Array c = {1, 2}; // T = int Array d = {1.0, 2.0}; // T = double
But I failed to write a working deduction guide for multidimensional ones:
Array e = {{1, 2}, {3, 4}}; // Expects T = int Array f = {{1.0, 2.0}, {3.0, 4.0}}; // Expects T = double
Is there any way to write a type deduction guide for this class?