I would like to know the best way to define a std::array type with with the additional feature of memory alignment using modern C++11. I understand that alignas
cannot be used with type aliases but this is the spirit of what I'm trying to do:
template<class T, std::size_t N, std::size_t A>
using AlignedArray = alignas(A) std::array<T, N>;
which could be instanted like this:
AlignedArray<int8_t, 4, 32> MyArray;
The best working version I could come up with is this:
template<class T, std::size_t N, std::size_t A>
struct alignas(A) AlignedArray : std::array<T, N> {
using std::array<T, N>::array;
};
Can anyone suggest anything simpler or better? I'm new to memory alignment issues so any advice would be appreciated.
As was noted in an earlier question, you can do this of course:
alignas(32) std::array<int8_t, 4> MyArray;
But that does not define a reusable type so it isn't what I'm looking for in this question.