sim642•

I coded something similar once, except with the additional feature of very easily initializing it: https://gist.github.com/sim642/9184484. Also a base case of 0 dimensions sounded much more neat to me, less duplicate code.

2
1BadOP•

Why does dimension of 0 reduce to Type, rather than dimension of 1 reducing to type, and zero becoming nullptr_t or something else?

1
sim642•

One dimension means normal one dimensional vector, very simple.

2
1BadOP•

Oh ok, that makes sense from a mathematic view to differentiate scalars from 1D vectors.

1
1BadOP•

I thought someone else might think this was a cool neat trick with alias templates with SFINAE.

1†
remotion4d•

So what you really do is this:

std::vector<std::vector<std::vector<T>>> 

This is CPU Cache-Trasher and of course Memory-Waster.

2
1BadOP•

Compared to allocating a single block of memory?

2
remotion4d•

Yes compared to flat/packed array/vector.

Here is more info.

LibFlatArray

1
Houndie•

I mean if you JUST want a flat array (array-of-structs style), there's boost::multi_array. There's also boost::ublas::numeric::matrix which has way too many namespaces in front, but has fairly nice syntax if all you need is a 2D solution.

2
gentryx•

Hi, I'm the developer of LibFlatArray. It's true that you could use LibFlatArray::soa_grid as an alternative for boost::multi_array, but that's not its intended use. The "soa" stands for Struct of Arrays. The library's main purpose is to store 1D/2D/3D arrays of objects (structs) in a "struct of arrays" format. These arrays comprise a single member of all objects. In fact we only use a single array, thereby eliminating the struct altogether.

Advantages: kernels which iterate through such an array can be efficiently vectorized and address calculation is greatly simplified. Despite not actually storing objects, the library provides an object-oriented interface.

Drawback: increased compile times (may jump from seconds to minutes).

2
1BadOP•

Cool, I will keep that in mind if I ever need a super high performance solution. Are there benchmarks for LibFlatArray? I searched their site and couldn't find any

I posted this more as an accessible way to expose people to two common SFINAE patterns, and thought hiding it with an alias was interesting, since aliases don't support SFINAE themselves. Also, like you linked to, a lot of people have problems with the syntax for creating multi-dimensional arrays, so I wanted to point out a simple way to ease their pain.

This is just about 50 lines of code to make the standard libraries more accessible. The standard library is designed for usability and "good enough" speed. If someone is working in high performance computing they obviously aren't using this or the standard libraries for their performance critical code.

1