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

问题描述:

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

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

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

auto my_shape = boost::extents[3][4][5]; 
intArray3D xs(my_shape), ys(my_shape), zs(my_shape); 

这是很容易使用auto分配boost::extents[3][4][5]到一个变量,但如何能我具体弄清楚了底层类型?

+1

你想知道它的好奇心的缘故,还是你想然后用它编码? – Quentin

+1

你的情况是['boost :: detail :: multi_array :: extent_gen '](https://github.com/boostorg/multi_array/blob/83c3738519a442c619f9ef661335acde6878b7d8/include/boost/multi_array/extent_gen.hpp#L28),但你为什么在意? – Praetorian

+0

@Quentin实际上,两者都不是。我正在构建一个需要一些未知但可计算大小的'multi_array'的类,并且我认为在某处存储这样一个'extent'可能会使它们的构建更容易。我认为没有任何方式存储它(除了'auto'),所以我的好奇心促使我问! –

documentation该提到:

template gen_type<Ranges>::type

  • 这种类型的发生器是用来指定的Ranges链式调用extent_gen::operator[]结果。

其中gen_typeboost::multi_array_types::extent_gen的部件(和boost::multi_array_types::extent_gen也是全球辅助对象boost::extents的类型)。

您还可以看到,接受一组范围的构造函数是以这种方式指定的(至少为了公共文档的目的)。 For example

namespace boost { 

template <typename ValueType, 
      std::size_t NumDims, 
      typename Allocator = std::allocator<ValueType> > 
class multi_array { 

...

typedef multi_array_types::extent_gen   extent_gen; 

...

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

所以,你可以重写该行代码,而无需使用auto为:

boost::multi_array_types::extent_gen::gen_type<3>::type my_shape = 
    boost::extents[3][4][5]; 

这是一点点对于一个局部变量愚蠢,但也许你想在一个类或类似的东西中存储一组范围。如果是这样,这是根据正式记录的界面来做到这一点的方法。

(正如评论指出,实际类型这个类型定义解析为涉及boost::internal::,但你永远不应该使用任何在你的代码中的“内部”的命名空间,因为这有可能在未来版本中的更改。)

+0

我不明白为什么所有的并发症是必要的。文件清楚地表明任何容器都是允许的。这包括像'std :: array '的东西。 'extent_gen'只是语法糖的一种内部类型。 (请参阅[我的答案](https://*.com/a/46596425/85371)) – sehe

+1

这是一个很好的观点。我并不熟悉'multi_array',并且关注什么'extents [3] [4] [5]'是甚至没有查看它们用于的实际数组的问题。 – aschepler

我会存储

template<class T> 
using factory=std::function< T() >; 

然后当我想创造出许多阵列:

auto my_shape = boost::extents[3][4][5]; 
factory<intArray3D> shaper = [my_shape]{ return intArray3D(my_shape); }; 
intArray3D xs(shaper()), ys(shaper()), zs(shaper()); 

这消除了对电子的依赖xact类型的提升范围。

最重要的是,

  1. 你不必知道
  2. 你不必使用extents无论是

很多事情都是acceptible只要它们满足documented criteria

enter image description here

收集概念在that link

记录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); 
    } 

} 

打印

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