【问题标题】:Estimate dimensions of a functions parameter array估计函数参数数组的维度
【发布时间】:2017-07-18 11:11:58
【问题描述】:

理论上应该在编译时知道类型,编译器也知道维度。目前,我有一个模板函数,它将矩阵的维度作为模板参数。我可以通过估计 constexpr 中的尺寸或通过模板函数以某种方式避免这种情况吗?

struct cont {};
void ffd<3>::run(cont mat[3][3][3])

在理解中,我想避免声明rows 参数。

template<uint8_t rows>
struct ffd {
  template<class T>
  static float run(const T &mat) {
      // recursion over the rows in mat
  }
};

【问题讨论】:

  • 所以你想在编译时从T 得到rows
  • 我的第一个想法是这也不应该是可能的。但是类型在编译时是已知的,不是吗?
  • 我可以看到 3 个模板参数的 3 个实例,除了维数也是 3。void ffd&lt;4&gt;::run 的参数是什么? void ffd&lt;4&gt;::run(float mat[4][4][4])void ffd&lt;4&gt;::run(float mat[3][3][3][3]) 还是别的什么?目前您的问题不清楚...
  • 不清楚

标签: c++ templates constexpr


【解决方案1】:

你要搜索的是std::extent:

template< class T, unsigned N = 0>
struct extent;

哪个

如果 T 是数组类型,则提供等于 沿数组第 N 维的元素数,如果 N 在 [0, std::rank::value)


例如,对于

float a[10][11][12];

当调用run(a):

template<class T>
float run(const T &mat)
{
   std::extent<T, 0>::value; // 10
   std::extent<T, 1>::value; // 11
   std::extent<T, 2>::value; // 12

}

【讨论】:

  • @dgrat 您遇到了与上一个示例相同的问题。这里数组衰减到指针发生在proxy的参数处。proxy的真正参数类型是int (*)[5][6]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 2015-06-30
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
相关资源
最近更新 更多