Short question: Is there a shorter way to do this
array<array<atomic<int>,n>,m> matrix;
I was hoping for something like
array< atomic< int>,n,m> matrix;
but it doesnt work...
In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float x[3][4];
A template alias might help out:
#include <array>
template <class T, unsigned I, unsigned J>
using Matrix = std::array<std::array<T, J>, I>;
int main()
{
Matrix<int, 3, 4> matrix;
}
A palatable workaround for compilers that don't support template aliases yet is to use a simple metafunction to generate the type:
#include <cstddef>
#include <array>
template<class T, std::size_t RowsN, std::size_t ColumnsN>
struct Matrix
{
typedef std::array<std::array<T, ColumnsN>, RowsN> type; // row major
private:
Matrix(); // prevent accidental construction of the metafunction itself
};
int main()
{
Matrix<int, 3, 4>::type matrix;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With