Your patch

Mark Mitchell mark@codesourcery.com
Fri Mar 17 15:04:00 GMT 2000


  2000-03-17  Nathan Sidwell  <nathan@codesourcery.com>

	* typeck.c (require_complete_type): Don't assume size_zero_node.
	(complete_type_or_else): Likewise.

This patch broke the following small test-case:

  template <class T>
  struct S
  {
    int x[0];
  };

  template struct S<int>;

The bug is that we're trying to use a zero size to indicate both an
incomplete partial instantiation and a legitimate zero-sized entity
(namely an array with no elements.)  Once again, we see how extensions
can bite us.  

The zero-sized array extension has certainly caused far more trouble
in C++ land than it merits.  In C++, you can ever overload operator
new for a structure to automatically allocate the right number of
elements.  For example:

  struct S {
    int x[1];

    void* operator new (int n, size_t s) {
      return ::operator new (s + (n - 1) * sizeof (int));
    }
  };

  new (2) S; // Creates an S where x has room for two elements.

You have to do this anyhow, if you expect dynamic allocation to work,
and it doesn't make sense to allocate these objects in any way that's
not dynamic.  So, the only argument for keeping this extension is
compatibility with GNU C.  But, you can always compile GNU C programs
with GNU C, so for this to matter, you're taking little bits of GNU C
programs and putting them in your C++ programs.  That doesn't work in
general anyhow, and I see no reason to support this particular
anomalous behavior.

In addition, there are all kinds of nasty issues about template
instantiation where zero-sized arrays are concerned; fundamentally,
you can't provide zero-sized arrays as an extension without changing
the meaning of conforming programs.

I think we should just remove this extension, in C++.  But, if Jason
objects, then, Nathan, we need to find a way to separate the two kinds
of zeroness.  I think integer_zero_node was used instead of
size_zero_node exactly for this reason.  Nathan, why did Kenner's
change result in incomplete types having zero-sized, but not
size_zero_node, types for their size?  Jason, do you object to
removing this extension?

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com


More information about the Gcc-bugs mailing list