GCC 4.8.4 will compile the code fine.
Not every compiler does, MSVC++ 14 (VS 2015) doesn't compile
LayerData in({rand(),rand(),rand(),rand(),rand()});
But it does compile
LayerData in{{rand(),rand(),rand(),rand(),rand()}};
Using C++ 11 Universal notation
Looking at the reason GCC permits this, it seems to be because it allocates the type on the stack and then uses XMM to move 32 bits (64 when compiled against x64) at a time into the memory location. Maybe this changes with the size of the type, but generally I'd say if you need to enforce the same length you shouldn't be exposing the type like this anyways. Do note that universal notation can actually be used to your advantage and with templates you can form something that looks a lot similar to what you're trying to do and enforces the same amount of arguments:
#include <cstdint>
template <int N>
class LayerData
{
static_assert(N > 0, "Number must be greater than 0");
public:
uint32_t d[N];
template<typename... Args>
LayerData(Args&&... args) : d{uint32_t(args)...}
{
static_assert(sizeof...(Args) == N, "Invalid number of constructor arguments.");
}
};
int main()
{
LayerData<4> in1{1, 2, 3, 4}; //Fine
LayerData<60> in2{1, 2, 3, 4}; //Causes error "Invalid number of constructor arguments."
}
Hastily written, but you should get the idea.
int a[3] = {1};.int a[3] = {1};to also fail. But in the answer below, it is quoted form the standard that it is not.