Search code examples
c++boostboost-multi-array

Converting boost multi_array to its native array type


I am writing a class that acts as a go-between for c++ classes and legacy c code. I have been using boost multi_array's to simplify a lot of the code. This mult_array is declared as such:

using Array = boost::multi_array<float,2>

However, I have run into a problem where I need to pass my multi_array to a legacy function that has a signature similar to

void function(float param[ROWS][COLS]);

My multi_array is of size ROWS and COLS, but I do not know of any easy way to convert the mutli_array to an array. Is there any way to do so?


Solution

  • Since the storage order of boost::multi_array is well defined, you can actually call it safely like that:

    function((float (*)[COLS])array.data());
    

    c_storage_order is the default, make sure to not use anything else upon construction of the object.