A filled_multi_array Template Function for Boost.MultiArray in C++
It looks bad, but that's not your fault. boost::multi_array just has a terrible interface. In any case, you want to avoid passing the shape twice, because this can give problems:
auto large_array = filled_multi_array(boost::extents[1][2][3][4][5], 1.0);
auto small_array = filled_multi_array(boost::extents[1], 1.0);
auto another_array = filled_multi_array&lt;double, large_array.dimensionality&gt;(small_array.shape(), 2.0);

The above will compile without errors, but might crash at runtime, since small_array.shape() is a pointer to an array that doesn't have as many elements as expected. I don't know why the interface is so terrible, if they would have .shape() return a (reference to a) boost::array&lt;size_t, NumDims&gt; it would all be checkable at compile time and without needing reinterpret_casts.
So to prevent errors, you want to avoid having to pass two separate pieces of information. One possibility is just passing the array whose shape you want to copy as an argument:
template&lt;class T, std::size_t NumDims&gt;
auto filled_multi_array(const boost::multi_array&lt;T, NumDims&gt; &amp;input, const T&amp; value) {
    boost::multi_array&lt;T, NumDims&gt; output(reinterpret_cast&lt;boost::array&lt;size_t, NumDims&gt; const&amp;&gt;(input.shape()));
    std::fill_n(output.data(), output.num_elements(), value);
    return output;
}

But that interface will create confusion: does the following line modify the input?
filled_multi_array(filled_multi_array1, 2.0);

Another possibility is to keep just the first filled_multi_array() variant, but create a helper function to create a extent_gen from a multi_array:
template&lt;std::size_t NumDims&gt;
auto get_extents(const boost::multi_array &amp;input) {
    boost::detail::multi_array::extent_gen&lt;NumDims&gt; output;

    // Set the shape of output according to input.shape(),
    // left as an exercise to the reader.
    ...

    return output;
}

And then you can use it like so:
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);