Say I have
#include <boost/multi_array.hpp>
using intArray3D = boost::multi_array<int, 3>;
and I want to create a bunch of intArray3D
s with the same shape:
auto my_shape = boost::extents[3][4][5];
intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
It's easy enough to use auto
to assign boost::extents[3][4][5]
into a variable, but how can I concretely figure out the underlying type?
Most importantly,
extents
eitherMany things are acceptible as long they satisfy the documented criteria:
The Collection concept is documented in 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);
}
}
Prints
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
The documentation mentions:
template gen_type<Ranges>::type
- This type generator is used to specify the result of
Ranges
chained calls toextent_gen::operator[]
.
where gen_type
is a member of boost::multi_array_types::extent_gen
(and boost::multi_array_types::extent_gen
is also the type of the global helper object boost::extents
).
You can also see that constructors which accept a set of extents are specified in this way (at least for purposes of public documentation). 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());
So you could rewrite that line of your code without using auto
as:
boost::multi_array_types::extent_gen::gen_type<3>::type my_shape =
boost::extents[3][4][5];
This is a bit silly for a local variable, but maybe you want to store a set of extents in a class or something like that. If so, this is the way to do it according to the officially documented interface.
(As noted in comments, the actual type this typedef resolves to involves boost::internal::
, but you should never use anything from an "internal" namespace in your code, because that is subject to change in future versions.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With