This is a follow-up question for A filled_multi_array Template Function for Boost.MultiArray in C++. Based on G. Sliepen's answer, there is another get_extents template function mentioned for handling retrieving boost::detail::multi_array::extent_gen<NumDims> type size information from a multi_array. I am trying to implement this get_extents template function here.

template<class T, std::size_t NumDims>
auto get_extents(const boost::multi_array<T, NumDims>& input) {
    boost::detail::multi_array::extent_gen<NumDims> output;
    std::vector<size_t> shape_list;
    for (size_t i = 0; i < NumDims; i++)
    std::copy(shape_list.begin(), shape_list.end(), output.ranges_.begin());
    return output;

I am trying to copy shape content into the output object. In general cases, boost::detail::multi_array::extent_gen object is always constructed by boost::extents[][][].... However, I am not sure how to use boost::extents in this context and I fill the values of shape information into output.ranges_ instead. It works with the following testing code.

auto filled_multi_array1 = filled_multi_array(boost::extents[3][4][2], 1.0);
auto filled_multi_array2 = filled_multi_array(get_extents(filled_multi_array1), 2.0);

//  Checking shape
std::cout << "filled_multi_array2.shape()[0]:" << filled_multi_array2.shape()[0] << std::endl;
std::cout << "filled_multi_array2.shape()[1]:" << filled_multi_array2.shape()[1] << std::endl;
std::cout << "filled_multi_array2.shape()[2]:" << filled_multi_array2.shape()[2] << std::endl;

for (decltype(filled_multi_array2)::index i = 0; i != filled_multi_array2.shape()[0]; ++i)
    for (decltype(filled_multi_array2)::index j = 0; j != filled_multi_array2.shape()[1]; ++j)
        for (decltype(filled_multi_array2)::index k = 0; k != filled_multi_array2.shape()[2]; ++k)
            std::cout << filled_multi_array2[i][j][k] << std::endl;

All suggestions are welcome.

The summary information:

  • Which question it is a follow-up to?

    A filled_multi_array Template Function for Boost.MultiArray in C++

  • What changes has been made in the code since last question?

    The extent_gen function implementation is the main part in this question.

  • Why a new review is being asked for?

    The shape_list which type is std::vector<size_t> in get_extents function is used just for copying shape data into output.ranges_. The reason why I create a std::vector for this operation is that there is neither .begin() function nor .end() function in input.shape(). Something like std::copy(input.shape().begin(), input.shape().end(), output.ranges_.begin()); doesn't work. If there is any possible improvement, please let me know.

  • \$\begingroup\$ May I ask, What are you developing? \$\endgroup\$
    – user228914
    Commented Nov 10, 2020 at 21:33
  • \$\begingroup\$ Some programs for multidimensional data processing experiment. \$\endgroup\$
    – JimmyHu
    Commented Nov 10, 2020 at 23:32

1 Answer 1


Avoid unnecessary temporary variables

There still is no need for the temporary vector shape_list, just use std::copy_n:

boost::detail::multi_array::extent_gen<NumDims> output;
std::copy_n(input.shape(), NumDims, output.ranges_.begin());

Please take some time to look at the list of all the STL algorithms. There are lots of useful functions there that will save you a lot of time. You don't have to remember them all, but get a feel for the type of functionality it provides, and next time you have a problem you might remember that some existing algorithm might solve it for you. The World Map of C++ STL Algorithms is also a great resource to build intuition for it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.