Home
ObjexxFCL 4.2
 

MArray

The MArray class template hierarchy provides Fortran-compatible "member" arrays, which allow members of objects in arrays to be treated as arrays themselves. MArrays are proxies giving access to the members of other objects.

struct C
{
   int m;
};

Array1D< C > a( 5 ); // Array of C objects

// Different ways to make a member array for the m members of the elements of a
MArray1< Array1D< C >, int > am( a, &C::m ); // Now am( i ) == a( i ).m
auto am( make_MArray1( a, &C::m ) ); // Simpler way with maker function
auto am( MA1( a, &C::m ) ); // Shorthand maker function
auto am( a.ma( &decltype( a )::Value::m ) ); // Array ma method alternative

It would be cleaner if, as in Fortran, we could treat the member array like a built-in array member or method, so that we could refer directly to a.m as an array and a(i).m as an element. Since Arrays are not built into C++ the compiler cannot do this for us. Instead, a method for adding such project-specific methods is provided by included files such as Array1.Project.MArray.hh.

Tip

Member arrays are a nice syntactic convenience provided by Fortran but they can also cause confusion by making it look like something is not an array or that something is a member of an array instead of its elements. Member arrays can also have performance impacts due to the construction time and extra lookup overhead due to the extra level of indirection. For these reasons, and since they are not emulated as cleanly as in Fortran, member arrays are best avoided and would be good to migrate away from following a Fortran-to-C++ conversion.