This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ PATCH to reject initializating flexible array members in constructors (PR c++/72775)


On 12/13/2016 12:06 PM, Jason Merrill wrote:
On 12/09/2016 07:46 PM, Martin Sebor wrote:
On 12/09/2016 02:49 PM, Jason Merrill wrote:
On Fri, Dec 9, 2016 at 11:33 AM, Martin Sebor <msebor@gmail.com> wrote:
For flexible array members, because they're not in C++, we get to
make up the rules that make the most sense to us.  IMO, they should
fit in well with the rest of the language.

I disagree; we should support C code, but flexible arrays don't really
fit with the C++ object model, so I don't think trying to do anything
clever with them in constructors is worthwhile.

With the suggested approach the array becomes just an ordinary member.
It's not a flexible array member anymore because its bound is deduced
from the initializer (it just looks like one).  The NSDMI char a[] =
"foo" syntax is just a shorthand for char a[4] = "foo".

That would mean that the size of the class varies with its initializer,
which again gets into not fitting with the C++ object model.

Okay, I accept the challenge of finding a case where the size
of a class depends on an initializer expression :)

Here's one:

  constexpr long f (int) { return 1; }
  constexpr long f (long) { return LONG_MAX; };

  struct A {   // sizeof (A) == 4
    enum E { i = f (0) } e;
  };

  struct B {   // sizeof (B) == 8
    enum E { i = f (0L) } e;
  };

It's not exactly the same but I think it's close enough to argue
that deducing the array size from its initializer wouldn't go
against the established model.

FWIW, I have a (vague) recollection of an incident where the enum
size problem caused ABI breakage in a product (it was in C and
involved simple macros).  Because an enum is normally expected
to be the same size as an int, having its size change based on
the (hidden behind a macro) value of one of its enumerators is
likely far more unexpected than having an array size depend on
its its initializer.

Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]