[comp.lang.c++] unbounded static array member

daniel@saturn.ucsc.edu (Daniel Edelson) (03/09/90)

Is there or should there be a syntax for declaring a static array 
member whose length is determined by the length of its initializer list?

e.g.,

	struct S 
		static int array[];		// Error, bound required
	};

	int S::array[] = { 1, 2, 3, 0};

It would also be OK if the array variable were of pointer type, e.g.,

	struct S {
		static int * array;
	};

	int S::array[] = { 1, 2, 3, 0};	// Error, type mismatch int * and int []


	struct S {
		static int * array;
	};

	int * S::array = { 1, 2, 3, 0};	// Error, initializer list too long


The only way around this that I've found requires an extra variable.

	struct S {
		static int * array;
	};

	int table[] = {1, 2, 3};

	int * S::array = table;

But it would be preferable to avoid the extra variable.

daniel@cis.ucsc.edu

shap@delrey.sgi.com (Jonathan Shapiro) (03/10/90)

In article <11024@saturn.ucsc.edu> daniel@saturn.ucsc.edu (Daniel Edelson) writes:
>Is there or should there be a syntax for declaring a static array 
>member whose length is determined by the length of its initializer list?
>
>e.g.,
>
>	struct S 
>		static int array[];		// Error, bound required
>	};
>
>	int S::array[] = { 1, 2, 3, 0};

No.  Such an array is not (by definition) fixed size given only the
declaration to work from.  In C/C++, that's what pointers are for.

What IS needed is a way to define class objects that act like arrays.
These need a suitable constructor that is not now found in the
language for static initialization:

	class Foo;

	class FooArray {
		...
	public:

		FooArray[](int, const Foo *const);
	};


	FooArray myFooArray = { foo, foo, foo, foo, foo };

The claim is that we should model the array initialization syntax as a
pair of the form

	(length, pointer-to-base)

In the absence of this one cannot build things like static
bounds-checked arrays that are initialized without introducing extra
variables.

On the other hand, it's definitely a marginal problem, and this idea
is at best half-baked.

Jonathan Shapiro
Silicon Graphics

rfg@ics.uci.edu (Ronald Guilmette) (03/10/90)

In article <11024@saturn.ucsc.edu> daniel@saturn.ucsc.edu (Daniel Edelson) writes:
>
>Is there or should there be a syntax for declaring a static array
>member whose length is determined by the length of its initializer list?
>
>e.g.,
>
>	struct S
>		static int array[];		// Error, bound required
>	};
>
>	int S::array[] = { 1, 2, 3, 0};

That works fine using cfront 2.0!  What's the problem?  Oh, well!  So what
if it isn't documented!  Next thing, you'll be wanting to have your cake
and eat it too! :-)

Apparently, cfront 2.0 treats declarations of static data members kind of
like extern declaration of global data objects, at least to the extent
that they may have incomplete (array) types (as in the example above).

The thing that has been puzzling me is why, if the above example is legal,
the following example is not:

	struct incomplete;	// an incomplete type

	struct defined {
		static struct incomplete data_member;
	};

	struct incomplete {
		...
	};


// Ron Guilmette (rfg@ics.uci.edu)
// C++ Entomologist
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.

rfg@ics.uci.edu (Ronald Guilmette) (03/10/90)

In article <5104@odin.SGI.COM> shap@delrey.sgi.com (Jonathan Shapiro) writes:
>In article <11024@saturn.ucsc.edu> daniel@saturn.ucsc.edu (Daniel Edelson) writes:
>>Is there or should there be a syntax for declaring a static array
>>member whose length is determined by the length of its initializer list?
>>
>>e.g.,
>>
>>	struct S
>>		static int array[];		// Error, bound required
>>	};
>>
>>	int S::array[] = { 1, 2, 3, 0};
>
>No.  Such an array is not (by definition) fixed size given only the
>declaration to work from.  In C/C++, that's what pointers are for.
...
>On the other hand, it's definitely a marginal problem, and this idea
>is at best half-baked.

Apparently the people at AT&T disagree, since it has been implemented.


// Ron Guilmette (rfg@ics.uci.edu)
// C++ Entomologist
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.