如何在模板类中为“boost::array_view”创建 typedef
-
23-12-2019 - |
题
我想要一个与维度无关的模板(对 3d 和 4d 都有用),大多数操作将在剥离第一维度的子矩阵上执行。
这就是我想要的
template <typename element, int dimensions>
class MMapMatrixTemplate{
public:
typedef boost::multi_array_ref<element, dimensions> array_type;
typedef std::array<size_t, dimensions> index_type;
typedef array_type::array_view<dimensions-1>::type stride_type;
};
在哪里 array_type
定义由该类管理的数组 index_type
定义用于索引数组的类型,我想 `stride_type
定义该数组的一个少一维的切片。
现在我收到一个错误:
include/MMapMatrix.hh:31:55: error: non-template ‘array_view’ used as template
typedef boost::multi_array_ref<element, dimensions>::array_view<dimensions-1>::type stride_type;
^
解决方案
来自 文档 在视图上,您可以看到视图类型的定义为:
typedef typename Array::template array_view<3>::type view1_t;
所以这会让你的代码编译:
#include "boost/multi_array.hpp"
template <typename element, int dimensions>
class MMapMatrixTemplate{
public:
typedef boost::multi_array_ref<element, dimensions> array_type;
typedef std::array<size_t, dimensions> index_type;
//typedef array_type::array_view<dimensions-1>::type stride_type;
typedef typename array_type::template array_view<dimensions-1>::type stride_type;
};
int main(int argc, const char *argv[])
{
typedef MMapMatrixTemplate<double, 4> matrix;
return 0;
}
您需要指定 array_view
实际上是一个 类模板 以便像这样使用它。否则,编译器希望它是完全定义的类型。
其他提示
你需要 typename
和/或 .template
从属名称的限定:
typedef typename array_type::array_view<dimensions-1>::type stride_type;
如果您在从属名称上使用模板成员,则需要 .template
资质:
obj.template foo<T>();
请参阅这个非常受欢迎的答案了解背景
我必须在哪里以及为什么必须放置“template”和“typename”关键字?
不隶属于 StackOverflow