Simple matrix for the rest of us

Why yet another Matrix?

Contents

Here is why I did make it.

In standard C++ community 2d array’s are the source of endless ongoing debate. Especially the large ones created on the heap.  While at the same time, approx 90% of use cases, in standard programs, do require, a plain old 2d array

// C/C++ plain old data structure
int matrix[3][3];

But, It is a bit dangerous to let your team use this “naked”. It is not that comfortable if you use them regularly and it is dangerous since the stack space can deplete very fast.

As visible in the code behind this API, standard C++ is giving us quite a few mechanisms and features to encapsulate the rules of using such simple native matrices. Making it

Fast Safe and Comfortable

Actual usage might be a bit unusual because instances are not necessary. Just the types. Example

To use the test function declared as above, we pass our matrix as a type and the rest of arguments as value:

This is a very fast solution

Above means, there are no copy or move mechanisms being used whatsoever,  No complex, computation intensive, matrix copying or swapping. The matrix simply stays in one place, where it was first created. Much like std::array conceptually works.

If one needs, to pass instances of this matrix in and out of function, as values, this is perfectly unnecessary but perfectly doable:

And the usage of the above function follows:

We receive argument as value,   we return the matrix as value. It totally does not matter. There is no instance bound data inside, Compiler has no job to do. Copy/move elision is not applicable. There is nothing to copy or move. No instances are necessary, get used to it :)

There are more usage samples,  all in the testing code provided.

The ‘your_print_function’ from above (same as dbj::console::print) is this