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

3
  • What doesn't work? What is the error message ? That would help someone to really answer the question. I understand template errors are themselves verbose, but at least a few would help.
    – Mahesh
    Commented Oct 7, 2011 at 15:18
  • Well it's missing a > for starters.
    – Mat
    Commented Oct 7, 2011 at 15:19
  • array<int,5,8> test; wrong number of template arguments (3, should be 2) Commented Oct 7, 2011 at 15:20

5 Answers 5

32

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;
}
8
  • 1
    very nice, I was scared that MACRO black magic is the only way(to HELL) :P Commented Oct 7, 2011 at 15:36
  • 3
    I tested it on clang/OS X before posting. Commented Oct 7, 2011 at 15:36
  • 7
    Howard's example is somewhat confusing to me because the matrix does not map to int[3][4]. Instead, it is like int[4][3]. The definition int[4][3] is an array of 4 elements where each element is an array of 3 integers. That's what is defined above using std::array. Consistent witht that thinking I and J should be swapped in the template alias of Matrix. Better yet, they should renamed Rows and Columns.
    – Sumant
    Commented Oct 10, 2011 at 22:07
  • 1
    Tip of tree Clang supports template aliases as well. Commented Oct 11, 2011 at 12:50
  • 1
    Just to be clear, the suggestion to reverse the order of the template parameters has been applied in the answer.
    – Steven Lu
    Commented Apr 8, 2020 at 5:45
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;
}
12

Solution using variadic templates (slightly more complex than the template alias, but more general purpose)

template <typename T, std::size_t thisSize, std::size_t ... otherSizes>
class multi_array : private std::array<multi_array<T, otherSizes...>, thisSize>
{
 using base_array = std::array<multi_array<T, otherSizes...>, thisSize>;

 public:
    using base_array::operator[];
    // TODO: add more using statements to make methods
    // visible.  This is less typing (and less error-prone)
    // than forwarding to the base_array type.
};

template <typename T, std::size_t thisSize>
class multi_array<T, thisSize> : private std::array<T, thisSize>
{
 using base_array = std::array<T, thisSize>;

 public:
    using base_array::operator[];
    // TODO: add more using statements to make methods
    // visible.  This is less typing (and less error-prone)
    // than forwarding to the base_array type.
};

There might be some improvement on assigning to non-leaves of the array that could be made.

I tested with a relatively recent build of clang/LLVM.

Enjoy!

11

When nested, std::array can become very hard to read and unnecessarily verbose. The opposite ordering of the dimensions can be especially confusing.

For example:

std::array < std::array <int, 3 > , 5 > arr1; 

compared to

char c_arr [5][3]; 

Also, note that begin(), end() and size() all return meaningless values when you nest std::array.

For these reasons I've created my own fixed size multidimensional array containers, array_2d and array_3d. They have the advantage that they work with C++98.

They are analogous to std::array but for multidimensional arrays of 2 and 3 dimensions. They are safer and have no worse performance than built-in multidimensional arrays. I didn't include a container for multidimensional arrays with dimensions greater than 3 as they are uncommon. In C++11 a variadic template version could be made which supports an arbitrary number of dimensions (Something like Michael Price's example).

An example of the two-dimensional variant:

//Create an array 3 x 5 (Notice the extra pair of braces) 
fsma::array_2d <double, 3, 5> my2darr = {{ 
{ 32.19, 47.29, 31.99, 19.11, 11.19}, 
{ 11.29, 22.49, 33.47, 17.29, 5.01 }, 
{ 41.97, 22.09, 9.76, 22.55, 6.22 } 
}};  

Full documentation is available here: http://fsma.googlecode.com/files/fsma.html

You can download the library here: http://fsma.googlecode.com/files/fsma.zip

5
  • u rule, I was wondering why they didnt do this in std::, probably they wanted to keep it pure generic :) Commented Oct 29, 2011 at 13:41
  • 2
    btw why is .size() meaningless? Doesnt it behave as expected ? arr.size()==dim1, arr[0].size()=dim2 Commented Oct 29, 2011 at 13:42
  • 4
    Perhaps meaningless was too strong a word. What I meant was that it doesn't return the total number of elements in the multidimensional array. btw I agree that something like it should be in the standard. Would make a good alternative to unsafe built-in multdimensional arrays :)
    – Ricky65
    Commented Oct 29, 2011 at 18:51
  • Could you please explain how your elems array gets populated with the data in the initializer list without any constructor or assignment operators?
    – rozzy
    Commented Apr 25, 2013 at 18:04
  • It uses the default constructor so aggregate initialization is possible.
    – Ricky65
    Commented Apr 29, 2013 at 20:52
3

Here's a simple, generic version:

template <typename T, size_t d1, size_t d2, size_t... ds>
struct GetMultiDimArray
{
    using type = std::array<typename GetMultiDimArray<T, d2, ds...>::type, d1>;
};

template <typename T, size_t d1, size_t d2>
struct GetMultiDimArray<T, d1, d2>
{
    using type = std::array<std::array<T, d2>, d1>;
};

template <typename T, size_t d1, size_t d2, size_t... ds>
using MultiDimArray = typename GetMultiDimArray<T, d1, d2, ds...>::type;

// Usage:
MultiDimArray<int, 3, 2> arr {1, 2, 3, 4, 5, 6};
assert(arr[1][1] == 4);
1
  • Just be careful: methods like std::array::size() will not return the correct value. Also, methods like std::array::fill(value_type) will not work at all.
    – Ethan
    Commented Jun 21, 2013 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.