Alex Rivera | Logout

less verbose way to declare multidimensional std::array

Asked 2011-10-07T15:17:46.533
17

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...

Edit
Report

1 Answer

30

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;
}
answered 2011-10-07T16:06:22.520

Your Answer