Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialize an std::array of unnamed structs?

Tags:

The below works:

struct
{
    int v;
} vals[] = { {1}, {2} };

Can I do the same thing but instead initialize an std::array?

Edit since so many people are asking 'why'

There are some very obvious workarounds (listed in the comments), but I only want to use the type once, so I don't really want it added to my current namespace. I could use a tuple or something similar, but having the named values improves clarity. I don't need the c-array value if I am constructing an std::array, so I can't use decltype.

The cleanest solution that I want to do is:

struct
{
    int v;
} std::array vals = { {1}, {2} };

There is an element of academic interest as well - "is this possible in some way I hadn't though of?". Seems like not, so I'll likely use:

struct
{
    int v;
} c_array[] = {};

std::array<std::remove_reference_t<decltype(c_array[0])>, 2> arr = { {1}, {2} };