I don't think you can, since the dimensionality of multi_array
is encoded in the type's template arguments:
typedef boost::multi_array<double, 3> array_type;
Is fundamentally distinct from
typedef boost::multi_array<double, 2> array_type;
The "best" you might do is have
static const int MAX_DIMENSIONS = 10;
typedef boost::multi_array<double, MAX_DIMENSIONS> array_type;
And then build the extents to leave the "unused" dimension's extent at 1
:
Live On Coliru
#include <boost/multi_array.hpp>
#include <iostream>
static const int MAX_DIMENSIONS = 5;
typedef boost::multi_array<double, MAX_DIMENSIONS> array_type;
array_type make_array(int dims, int default_extent = 5) {
boost::array<int, MAX_DIMENSIONS> exts = {};
std::fill_n(exts.begin(), exts.size(), 1);
std::fill_n(exts.begin(), dims, default_extent);
return array_type(exts);
}
int main() {
auto a = make_array(3);
auto b = make_array(2);
std::copy(a.shape(), a.shape() + a.dimensionality, std::ostream_iterator<size_t>(std::cout << "\na: ", " "));
std::copy(b.shape(), b.shape() + b.dimensionality, std::ostream_iterator<size_t>(std::cout << "\nb: ", " "));
}
Prints
a: 5 5 5 1 1
b: 5 5 1 1 1