VLAs (variable length arrays) are a feature added to the C language in the 1999 ISO C standard (and made optional by the 2011 standard). The C++ standard has not adopted them.
VLAs in C++ are a gcc extension. They're closely based on the C version of the same feature. A large part of the reason g++ doesn't permit VLAs as class members is that C doesn't have classes. It does have structures, which are very similar to classes, but C doesn't permit VLAs in structures.
Implementing VLAs as objects with automatic storage duration (non-static
local variables) is relatively straightforward. The length of the array is determined by evaluating the expression between the [
and ]
when the declaration is encountered, and that determines how much space must be allocated (typically on the stack). Deallocating a VLA is typically done as part of tearing down the stack frame on leaving the block in which the object is declared.
A VLA as a member of a class or structure would be more complicated. In your example:
class PSO {
...
static int dim;
double position [dim];
double velocity [dim];
};
the position
and velocity
members would have to be allocated each time a PSO
object is created, using the current value of dim
to determine the length. If dim
has not had a value assigned to it, it's going to be 0
-- and neither C nor C++ permits zero-length arrays. If a value has been assigned, then the offset of velocity
will have to be computed at run time; that's not impossible, but normally members of classes and unions have constant offsets. Presumably the length of position
and velocity
would be determined by the value of dim
when the PSO
object is created -- but dim
can be modified later, making it difficult to determine the length of the arrays for an arbitrary PS0
object.
Applying sizeof
to a VLA object computes the size at run time. That size is typically stored in a compiler-created object associated with the type of the VLA. Each defined VLA object has its own type. If VLAs are permitted as class members, the number of distinct sizes can be arbitrary, as multiple PS0
objects are created dynamically.
None of these difficulties are insurmountable. Ada, for example, permits records (similar to C and C++ structures) with members that are arrays whose length can vary for each instance of the record type:
type Rec(Length: Natural) is
record
S: String(1 .. Length);
end record;
Supporting this as a language extension in C++ would require a great deal of work in the compiler. Presumably the gcc team, since they had to implement VLAs for C, decided that implementing them similarly for C++ would have a decent payoff without too much effort. Extending them as you suggest would have required considerable work (not least designing the semantics so they can be used consistently) for what they probably did not consider to be sufficient benefit -- especially since C++ has much more powerful features in the standard library container classes.
gcc's support for variable length arrays is described here, or type "info gcc" and search for "Arrays of Variable Length".