如何在使用模板的函数调用的大括号表达式中推导多维数组的大小

how to deduce the size of a multidimensional array in a curly brace expression of a function call with templates

本文关键字:表达式 数组 函数调用      更新时间:2023-10-16

我目前正在编写一个启用类型安全编译时间的张量类,它可以归结为

template<typename T, int... Sizes>
struct tensor {
T elements[(Sizes * ...)];
auto ptr() {return elements;}
auto size() {return (Sizes * ...);}
using value_type = T;
};

我想写一个方法,允许创建一个以数组为表达式的张量,比如:

make_tensor({1,2,3,4})make_tensor({{1,2},{3,4}}),给出一个向量和一个矩阵。我的make_tensor函数当前如下所示:

template<template<typename, int...> class Fill, typename Type, int... Sizes>
struct array_to_index {
using type = Fill<Type, Sizes...>;
};
template<template<typename, int...> class Fill, typename Type, int Size, int... Sizes>
struct array_to_index<Fill, Type[Size], Sizes...> {
using type = typename array_to_index<Fill, Type, Sizes..., Size>::type;
};
template<template<typename, int...> class Fill, typename Type>
using array_to_index_t = typename array_to_index<Fill, Type>::type;
template<typename Type, int Size>
auto make_tensor(const Type (&arr)[Size]) {
using tensor_t = array_to_index_t<tensor, Type[Size]>;
tensor_t tensor;
using ptr_t = const typename tensor_t::value_type *;
for(int i = 0; i < tensor.size(); ++i)
tensor.elements[i] = reinterpret_cast<ptr_t>(arr)[i];
return tensor;
}

如果我先分配它,我可以用多维数组调用它,但好的语法不起作用:

int main() {
//auto matrix = make_tensor({{0,1},{2,3},{4,5}}); // doesn't work
int arr[][2] = {{0,1},{2,3},{4,5}};
auto other = make_tensor(arr); // works
assert(other.elements[3] == 3);
}

根据我从这个多维数组初始化器中推导边界失败的问题中收集到的信息,我的方法将不起作用。有没有其他方法可以进行这种推导(例如使用初始值设定项列表(?

不能从嵌套的{{}}推导多维数组边界。

你可以通过添加一些标记来推断它。

using namespace std;
auto arr = array{ array{1,2}, array{3,4} };
for (auto row : arr) {
for (auto e : row) {
std::cout << e << ",";
}
std::cout << "n";
}

活生生的例子。

array推导指南撕开下面的{}表达式,并推导出array模板的类型。

因此:

make_tensor(a{a{1,2},a{3,4}})

是可能的,甚至

tensor{ tensor{1,2}, tensor{1,2} }

因为CCD_ 8函数是位传递函数。