提供维度后 boost::extent 对象的类型是什么?

Con*_*ser 5 c++ boost boost-multi-array

说我有

#include <boost/multi_array.hpp>
using intArray3D = boost::multi_array<int, 3>;
Run Code Online (Sandbox Code Playgroud)

我想创建一堆intArray3D形状相同的s:

auto my_shape = boost::extents[3][4][5];
intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
Run Code Online (Sandbox Code Playgroud)

使用auto赋值boost::extents[3][4][5]给变量很容易,但我如何具体找出底层类型?

seh*_*ehe 5

最重要的是,

  1. 你不必知道
  2. extents你也不必使用

只要满足记录的标准,许多事情都是可以接受的:

在此输入图像描述

集合概念记录在该链接中

Live On Coliru

#include <boost/multi_array.hpp>
#include <iostream>
using intArray3D = boost::multi_array<int, 3>;

void dump_shape(intArray3D const& arr) {
    for (unsigned dim = 0; dim < arr.dimensionality; ++dim)
        std::cout << arr.shape()[dim] << " ";
    std::cout << "\n";
}

int main() {
    {
        auto my_shape = boost::extents[3][4][5];
        intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
        dump_shape(xs); dump_shape(ys); dump_shape(zs);
    }

    {
        std::array<int, 3> my_shape { 3, 4, 5 };
        intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
        dump_shape(xs); dump_shape(ys); dump_shape(zs);
    }

    {
        std::vector<int> my_shape { 3, 4, 5 };
        intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
        dump_shape(xs); dump_shape(ys); dump_shape(zs);
    }

}
Run Code Online (Sandbox Code Playgroud)

印刷

3 4 5 
3 4 5 
3 4 5 
3 4 5 
3 4 5 
3 4 5 
3 4 5 
3 4 5 
3 4 5 
Run Code Online (Sandbox Code Playgroud)


asc*_*ler 3

文档提到

template gen_type<Ranges>::type

  • 该类型生成器用于指定Ranges对 的链式调用的结果extent_gen::operator[]

其中gen_type是 的成员boost::multi_array_types::extent_gen(也是boost::multi_array_types::extent_gen全局辅助对象 的类型boost::extents)。

您还可以看到接受一组范围的构造函数是以这种方式指定的(至少出于公共文档的目的)。 例如

namespace boost {

template <typename ValueType, 
          std::size_t NumDims, 
          typename Allocator = std::allocator<ValueType> >
class multi_array {
Run Code Online (Sandbox Code Playgroud)

...

  typedef multi_array_types::extent_gen         extent_gen;
Run Code Online (Sandbox Code Playgroud)

...

  explicit multi_array(extent_gen::gen_type<NumDims>::type ranges,
                       const storage_order_type& store = c_storage_order(),
                       const Allocator& alloc = Allocator());
Run Code Online (Sandbox Code Playgroud)

因此,您可以重写该行代码,而无需使用autoas:

boost::multi_array_types::extent_gen::gen_type<3>::type my_shape =
    boost::extents[3][4][5];
Run Code Online (Sandbox Code Playgroud)

对于局部变量来说,这有点愚蠢,但也许您想在类或类似的东西中存储一组范围。如果是的话,这是根据官方文档接口进行的方法。

(如注释中所述,此 typedef 解析为的实际类型涉及boost::internal::,但您永远不应该在代码中使用“内部”命名空间中的任何内容,因为这可能会在未来版本中发生更改。)