2 ms·
This would be a really useful feature in C if it was made more general, even if the scope of the definition remained limited. But with GCC 7 struct { int arr
by wahern 9y ago
This would be a really useful feature in C if it was made more general, even if the scope of the definition remained limited. But with GCC 7
struct { int arr[n]; } s = { 0 };
gives
error: variable-sized object may not be initialized
And
(struct { int arr[n]; }){ 0 }
gives
error: compound literal has variable size
GCC still doesn't even permit a simpler compound literal like (int[n]){ 0 }. I understand that compound literals and VLAs are distinct, and that the above limitations are, strictly speaking, consistent with the standard constructs. But I would think that once you have variable-length struct members that it wouldn't be too difficult to unify the semantics of VLAs and compound literals. If GCC can't be bothered to improve language consistency, then that's pretty strong evidence that VLA struct members are a dead-end and I can't fault clang for avoiding it.
But if GCC did improve consistency, it might significantly help push C toward safer array semantics. The last hurdle would be definition scope and working out how declarations worked. Presumably the ABI would require fat pointers that embed VLA annotations. Getting there would be a long, winding road, but this is how it would happen. It's complex, but the cost+benefit ratio would, I think, be better for both compiler writer and programmer compared to the complexity of C++.