Suppos I need to hold some data consisting of n Foo's, n Bars and n Bazs. The elements of corresponding indices are related, so you can also think of this data as n triplets of Foo, Bar andBaz`. The first description of the data is a "structure of arrays", the second is an "array of structures"; you can find a discussion of these two layouts here on StackOverflow.
Now, if we know n at compile time, it's pretty easy on your fiegers to express both perceptions in C++.
Array of Structures (AoS):
struct {
    Foo foo;
    Bar bar;
    Baz baz;
} aos_form[n];
Structure of Arrays (SoA):
struct {
    Foo foo[n];
    Bar bar[n];
    Baz baz[n];
} soa_form;
but note we are already breaking the DRY principle when we say n 3 times. This is greatly exacerbated if we now want to allocate memory for our data dynamically - say with std::unique_ptrs:
Array of Structures (AoS):
struct {
    Foo foo;
    Bar bar;
    Baz baz;
} data_tuple_t;
auto aos_form = std::make_unique<data_tuple_t[]>(n);
reasonable enough, although we seem to need a type definition now. But how about SoA? Well,
Structure of Arrays (SoA):
struct {
    std::unique_ptr<Foo[]> foo;
    std::unique_ptr<Bar[]> bar;
    std::unique_ptr<Bas[]> baz;
} soa_form = {
    std::make_unique<Foo[]>(n),
    std::make_unique<Bar[]>(n),
    std::make_unique<Baz[]>(n)
}
Now that's just terrible! We're repeating ourselves in 3 different ways:
- Specifying the field type twice - in type definition and in assignment
- Having to be "verbose" rather than "opaque" or "implicit" both in definition and in initialization.
- Having to specify the length thrice.
Other than writing a specially-customized class, what can I do more simply and generally to make the Soa FORM "easier on the eyes" (and the fingers)?
