Topic: Beta-reduction failure (Was: reading the standard (was char *p = new char[0]?))


Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 2000/01/30
Raw View
Steve Clamage wrote:

> No, a constant-expression used for the array element count must be
> greater than zero, but a non-constant expression is allowed to be
> zero.

I am the only person who sees that as a very weird inconsistancy ?

If new char[int(0.+0.)] were undefined behaviour, it would be
fine from the consistancy point of view. But consider beta-reduction,
the operation which turns

  (lambda (x) (... blah ... x ... blah ...)) some_value

into

  (... blah ... some_value ... blah ...)

With this strange rule, it doesn't work:

  void foo (int i) { new char[i]; }
  foo (0);

is fine, but

  new char[0];

is not.

--

Valentin Bonnard


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 2000/01/31
Raw View
Valentin Bonnard <Bonnard.V@wanadoo.fr> writes:

>Steve Clamage wrote:
>
>> No, a constant-expression used for the array element count must be
>> greater than zero, but a non-constant expression is allowed to be
>> zero.
>
>I am the only person who sees that as a very weird inconsistancy ?

No.  I, for one, agree.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3        |     -- the last words of T. S. Garp.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 2000/02/01
Raw View
ark@research.att.com (Andrew Koenig) writes:

 >Fergus Henderson <fjh@cs.mu.OZ.AU> wrote:
 >
 >>Valentin Bonnard <Bonnard.V@wanadoo.fr> writes:
 >
 >>>Steve Clamage wrote:
 >
 >>>> No, a constant-expression used for the array element count must be
 >>>> greater than zero, but a non-constant expression is allowed to be
 >>>> zero.
 >
 >>>I am the only person who sees that as a very weird inconsistancy ?
 >
 >>No.  I, for one, agree.
 >
 >It does sound weird.  But consider:
 >
 > // Example 1
 > int n = 0;
 > char* p = new char[n];
 >
 > // Example 2
 > char* p = new char[0];
 >
 > // Example 3
 > typedef char T[0];
 > char* p = new T;
 >
 >From a definitional viewpoint, how do you allow example 2 without
 >also allowing example 3?

I would argue (and indeed at the time I did)
in favour of allowing examples like example 3.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3        |     -- the last words of T. S. Garp.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/02/02
Raw View
Andrew Koenig wrote:
>
> In article <873e3t$9p1$1@mulga.cs.mu.OZ.AU>,
> Fergus Henderson <fjh@cs.mu.OZ.AU> wrote:
>
> >Valentin Bonnard <Bonnard.V@wanadoo.fr> writes:
>
> >>Steve Clamage wrote:
>
> >>> No, a constant-expression used for the array element count must be
> >>> greater than zero, but a non-constant expression is allowed to be
> >>> zero.
>
> >>I am the only person who sees that as a very weird inconsistancy ?
>
> >No.  I, for one, agree.
>
> It does sound weird.  But consider:
>
>         // Example 1
>         int n = 0;
>         char* p = new char[n];
>
>         // Example 2
>         char* p = new char[0];
>
>         // Example 3
>         typedef char T[0];
>         char* p = new T;
>
> >From a definitional viewpoint, how do you allow example 2 without
> also allowing example 3?

The same way I note that

int i=3;
typedef char T[i];
char* p = new T;

is illegal despite the fact that

int i=3;
char* p = new char[i];

is legal.

In short:

char[0] is not a valid type. But in new char[0], the type is char,
not char[0], so this rule doesn't apply here.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]