public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Variable-sized types vs. templates
@ 2002-10-08  1:45 Mark Mitchell
  2002-10-08  5:35 ` Nathan Sidwell
  2002-10-08 10:31 ` Jim Wilson
  0 siblings, 2 replies; 13+ messages in thread
From: Mark Mitchell @ 2002-10-08  1:45 UTC (permalink / raw)
  To: gcc; +Cc: nathan, jason, gdr

PR 5661 is a crash that arises when using a VLA as a template parameter.

Of course, VLAs are an extension to ANSI/ISO C++, so we are outside the
scope of the standard.

Here is the test case from the PR:

  template <class T> struct A{ A() throw() {} };

  struct B {
    static const int s;
    A<int[s]> a;
  };

  B b;

We accept "int[s]" thanks to the extension.  When trying to instantiate
A, we crash.

A purer example is:

  template <typename T> struct A { T t; };

  void f (int i) { A<int[i]> a; }

We happen not to crash on this example -- but we certainly do not do
the right thing either.

(Once again, we see the problem with incorporating extensions without
thinking through how they interact with other language features.  This
is likely not the only problem with variably-sized types.  I'll bet that
`typeid' and `dynamic_cast' do not behave sensibly in conjunction with
these types either.  The argument that GNU C (and now ANSI C) have VLAs
is, in my opinion, totally irrelevant; the C folks thought about what
these things mean in C, but nobody has thought through what they mean
in C++.)

However, I assume that nobody is about to let me remove the extension.

Therefore, we need to define a rule that allows us to diagnose the above
cases.  That's not easy.  For example, if T is a VLA type, or a type
containing a VLA, is "A<T>" ok?  What about "A<T*>"?  (Note that the
template body might dereference something of the pointer type.)  What
about "A<void (*)(T**)>"?  What about "A<S*>" where S is a struct
containing a VLA?

I do not fancy having to recursively walk the type tree every time we
instantiate a template; that will make our already slow template
instantiation process even slower.  Neither do I fancy having to keep
a bit on each and every type as we build them; the compiler creates
types in too many places using too many methods to make this easy to
get right.

Brilliant solutions?

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

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2002-10-11  3:25 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-08  1:45 Variable-sized types vs. templates Mark Mitchell
2002-10-08  5:35 ` Nathan Sidwell
2002-10-08  6:18   ` Jason Merrill
2002-10-08  6:22     ` Neil Booth
2002-10-08  7:07       ` Jason Merrill
2002-10-08  7:28         ` Richard Earnshaw
2002-10-08  7:31           ` Jason Merrill
2002-10-08  9:12             ` Richard Earnshaw
2002-10-11  3:20               ` Jason Merrill
2002-10-08  9:04       ` Andreas Schwab
2002-10-08 10:31 ` Jim Wilson
2002-10-08 10:59   ` Richard Earnshaw
2002-10-08 11:09   ` Jim Wilson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).