This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Variable-sized types vs. templates
On Tue, 8 Oct 2002 13:35:41 +0100, Neil Booth <neil@daikokuya.co.uk> wrote:
> Jason Merrill wrote:-
>
>> Similarly, I think it's appropriate to say that neither VLA nor VM types
>> are valid template type arguments. I'd also say that a VLA is not a valid
>> type for a struct member; I don't understand why C99 seems to allow that.
>> Perhaps it wants to allow it so that uses in different functions can have
>> different sizes, but that seems incompatible with the stronger C++ model of
>> struct types.
> I think they wanted to formalize hacks like:
>
> /* Chained list of answers to an assertion. */
> struct answer
> {
> struct answer *next;
> unsigned int count;
> cpp_token first[1];
> };
>
> Where first is an array of size determined at run time.
But first isn't a VLA. To actually use a VLA in a struct would mean an
example like
int foo_length;
struct foo
{
int arr[foo_length];
};
void f ()
{
foo_length = 42;
{
foo bar;
bar.arr[12] = 24;
}
}
which seems rather more ugly than the 'struct hack' you refer to. And,
since it can only be used for local variables, rather useless. Now that I
think of it, this is probably more what they had in mind:
void f (int n)
{
struct foo { ...; int arr[n]; } bar;
}
So, to refine my earlier suggestion, in C++ I'd prohibit VLAs in types with
linkage. A local class such as this is already an invalid template type
parm.
Jason