Nested Aggregate Initialization

closed account (3hM2Nwbp)
Looking at this sample:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <array>

template<typename T, uint32_t X, uint32_t Y>
class MatrixImpl
{
    static_assert(std::is_integral<T>::value || std::is_floating_point<T>::value, "The type for a Matrix must be numeric");
    public:
    std::array<std::array<T, Y>, X> values;
};

typedef MatrixImpl<int, 2, 2> Matrix2x2i;

int main()
{

//    No good, you've lost the one-glance dimensionality of the data.
//    Matrix2x2i test {0, 0, 0, 0};

//    Great, alas it does not compile!
/*
    Matrix2x2i test
    {
        {0, 0},
        {0, 0}
    };
*/

//    Any end-user would be beyond confused on the seemingly random # of braces required...
//    but it does compile!
    Matrix2x2i test 
    {{{
        {0, 0}, 
        {0, 0}
    }}};


}


Is there any legal way to clean up the aggregate initialization? I'm making a object oriented facade over opengl 4.x whose main goal is a simplistic, intuitive interface. Right now, I do not think that aggregate initialization is very intuitive...and if there isn't a cleaner way, I'm stripping the functionality.

Thanks for any input.
Last edited on
The last one actually doesn't compile under strict C++11 rules

test.cc:32:10: error: cannot omit braces around initialization of subobject when using direct list-initialization
        {0, 0}, 
         ^~~~
         {   }


(although this was a common enough non-standard extension that it became part of C++14)

In any case, I would provide a constructor that takes std::initializer_list
Topic archived. No new replies allowed.