1

suppose I have a bunch of two dimensional arrays of type bool[4][4] like this:

class A
{
public:
    static const int SIZE = 4;
    static constexpr bool blocks_S[SIZE][SIZE]={    {0,1,1,0},
                                                    {1,1,0,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_SR[SIZE][SIZE] = { {1,1,0,0},
                                                    {0,1,1,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_L[SIZE][SIZE] = {  {0,0,1,0},
                                                    {1,1,1,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_LR[SIZE][SIZE] =  {{1,1,1,0},
                                                    {0,0,1,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_Box[SIZE][SIZE] = {{1,1,0,0},
                                                    {1,1,0,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_Bar[SIZE][SIZE] = {{1,1,1,1},
                                                    {0,0,0,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};  
};

and I want to make an array called block_types that holds a reference to all these two dimensional arrays so I can for example do: block_types[0] and recieve: blocks_S. how can I construct such an array (without using std::vector etc.) and what should be the type block_types? I tried auto and it picked up the wrong type.

auto block_types = { A::blocks_S, A::blocks_SR,
                     A::blocks_L, A::blocks_LR,
                     A::blocks_Box, A::blocks_Bar};
2
  • Any reason you don't want to use std::vector? Is std::array fine? Commented Feb 10, 2019 at 4:55
  • the arrays are of fixed size, so I don't know in what way using std::vector or std::array would help. Commented Feb 10, 2019 at 10:14

1 Answer 1

5

The initializer for an array must be a braced list of element values -- this applies recursively. The initializer can't be another array. In other words, it's not possible to initialize multiple elements of an array by giving one initializer which is itself an array.

If you need two separate arrays with the same content you'll have to give them the same initializer: e.g. by writing it out twice, or using a #define macro, or generating the initializer by a constexpr function.


To fulfill your requirement:

I can for example do: block_types[0] and recieve: blocks_S

you don't need to create another array; you could use an array of pointers:

using row_t = const bool[A::size];
row_t *block_types[] = { A::blocks_S, A::blocks_SR,
                 A::blocks_L, A::blocks_LR,
                 A::blocks_Box, A::blocks_Bar};

after which block_types[0] can be used similarly to A::blocks_S.

Sign up to request clarification or add additional context in comments.

1 Comment

The best and simplest solution. Thanks. however I don't want to introduce macros to my code but if you could provide the solution using the constexpr function as well then it will be a comprehensive answer for the reference.

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.