2017-10-05 54 views
2

說我有提供維度後,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 <3>'](https://github.com/boostorg/multi_array/blob/83c3738519a442c619f9ef661335acde6878b7d8/include/boost/multi_array/extent_gen.hpp#L28),但你爲什麼在意? – Praetorian

+0

@Quentin實際上,兩者都不是。我正在構建一個需要一些未知但可計算大小的'multi_array'的類,並且我認爲在某處存儲這樣一個'extent'可能會使它們的構建更容易。我認爲沒有任何方式存儲它(除了'auto'),所以我的好奇心促使我問! –

回答

2

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://stackoverflow.com/a/46596425/85371)) – sehe

+1

這是一個很好的觀點。我並不熟悉'multi_array',並且關注什麼'extents [3] [4] [5]'是甚至沒有查看它們用於的實際數組的問題。 – aschepler

0

我會存儲

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類型的提升範圍。

3

最重要的是,

  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 
相關問題