我想制作一个NDArray
具有固定尺寸但可以在每个尺寸上调整大小的模板。
我的问题是如何使它能够根据使用了多少对来推断构造函数中的尺寸{}
?构造函数中的元素将用于初始化一些元素。
#include <array>
#include <iostream>
template<typename T, size_t Dimension>
class NDArray
{
T* buffer = nullptr; //flattened buffer for cache locality
std::array<size_t, Dimension> dimension; //keep the current sizes of each dimension
public:
NDArray(std::initializer_list<T> elements) : dimension{elements.size()} //for 1D
{
std::cout << "Dimension = " << Dimension << '\n';
}
NDArray(std::initializer_list<NDArray<T, Dimension-1>> list) //how to make this works???
{
std::cout << "Dimension = " << Dimension << '\n';
}
};
template<typename T, size_t N>
NDArray(const T(&)[N]) -> NDArray<T, 1>;
int main()
{
NDArray a{ {3,4,5} };//OK, NDArray<int, 1> because of the deduction guide
NDArray b{ {{1,2,3}, {4,5,6}} };//Nope, I want: NDArray<int, 2>
}