Is there a reason to prefere the usage of boost::multi_array_ref over boost::multi_array&? Why is the adapter class boost::multi_array_ref provided by boost at all?
1 Answer
Those would be two quite different things. boost::multi_array& is a reference which can be bound to already existing multi array, and you can use it for indirection to a 'true' multi array container.
On the other hand, boost::multi_array_ref is an adaptor - it allows one to use it with any other block of continuous data as if it was a multi array (while in fact it is not). (I personally think this is not a great name, as it implies a reference to the container)
See https://www.boost.org/doc/libs/1_73_0/libs/multi_array/doc/reference.html for more details.
3 Comments
UweJ
Do you know an example of a class etc. which needs this adapter?
SergeyA
@UweJ Not sure. Perhaps you can use it to access C-style arrays of array as if it was a
boost::multi_array? Haven't tried it myself.alfC
This is correct, it is also important to note also that in Boost.MA
multi_array IS-A ``multi_array_ref. Therefore a multi_array` can be bound to multi_array& but also to a multi_array_ref&!. In this sense, taking a function argument by multi_array_ref& if appropriate is more general than taking the argument by multi_array& (i.e. simply by reference). See godbolt.org/z/6TEbbPce7