2

How can I create a multi dimensional std::array of type Type with (of course) known initial constexpr dimensions with some smart variadic template "MDA".

The number of dimensions shall be variable.

In the end I want to be able to write:

MDA<int,3,4,5,6> a4d{};

and the result should be equal to

std::array < std::array < std::array < std::array<int, 6> , 5 > , 4 > , 3 > a4d{};

And save a lot of (complicated, or even error prone) typing work . . .


Edit:

I am not sure if this possible at all. But what I am looking for, is some "typing saver", maybe in conjunction with a using statement.

2
  • I can't help but notice, that every other "How to..." question of yours has an attempt of implementation clearly displayed, while this one is indeed seems like more of a request than a question on some concrete obstacle. Hence, the vote to close I guess. Commented Aug 7, 2021 at 10:59
  • Hm, since I am not a native english speaker, I may misunderstand your comment. I asked my question in good faith. I tried many embarrasing things, but I cannot find a solution. So, I am asking the community for help. I hope that this is allowed here on SO. If there is a misbehaviour on my end, I will delete the question. Although I am really interested in the solution . . . And obviously a "how to" question is obviously not good. I do not understand the subtelty of this, but will try to avoid that in the furture Commented Aug 7, 2021 at 11:14

2 Answers 2

5

Certainly possible with use of helper class templates, C++11 (except the static assert part)

#include <array>

template<typename T, std::size_t Head, std::size_t... Tail>
struct MDA_impl{
    using type = std::array<typename MDA_impl<T, Tail...>::type, Head>;
};
// Base specialization
template<typename T, std::size_t N>
struct MDA_impl<T,N>{
    using type = std::array<T, N>;
};

template<typename T, std::size_t... Ns>
using MDA = typename MDA_impl<T,Ns...>::type;

int main(){
    static_assert(std::is_same_v<MDA<int,3,4,5,6>,
                  std::array<std::array<std::array<std::array<int, 6>,5>,4>,3>>);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the so-called meta-functions.

template<class T, int N, int... M>
struct MDA_struct
{
    using type = std::array<typename MDA_struct<T, M...>::type, N>;
};

template<class T, int N>
struct MDA_struct<T, N>
{
    using type = std::array<T, N>;
};

template<class T, int... N>
using MDA = typename MDA_struct<T, N...>::type;

2 Comments

You cannot overload using statements. But your idea is correct, just use structs and partial specialization for the implementation details.
The code should now work correctly, see godbolt.org/z/ff6TnfEz7

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.