Topic: A couple of questions on new-expression with array initializer


Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Fri, 16 Jul 2010 16:19:00 CST
Raw View
Q1:

The FCD says that the following code is ill-formed. But we can't know that
until runtime

int n = 2;
new int[n]{1, 2, 3};

How is the runtime supposed to emit the diagnostic message? See 8.5.1/6 and
5.3.4/15:

6 An initializer-list is ill-formed if the number of initializer-clauses
exceeds the number of members or elements to initialize.

Hmm, GCC just segfaults. I wonder whether printing "segfault" is a valid
runtime diagnostic message. Any insights?

Q2:

Is the following valid?

new (int[]){1, 2, 3}

I know that without the parens, it's syntactically invalid. But with the
parens, "int[]" it's a type-id and valid, but it says at 5.3.4/1:

"The new-expression attempts to create an object of the type-id (8.1) or
new-type-id to which it is applied. The type of that object is the allocated
type. This type shall be a complete object type, ..."

At first sight it seems to forbid it, because the type-id specifies an
incomplete type. But the constraint is done upon the object-type. And that
type is, after gone through 8.5.1, "int[3]". GCC forbids it and says "error:
invalid use of array with unspecified bounds". Is GCC right? Is there any
clear wording about it?

Thanks for any insight.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Fri, 16 Jul 2010 17:24:50 CST
Raw View
On 17 Jul., 00:19, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> The FCD says that the following code is ill-formed. But we can't know that
> until runtime
>
> int n = 2;
> new int[n]{1, 2, 3};
>
> How is the runtime supposed to emit the diagnostic message? See 8.5.1/6 and
> 5.3.4/15:
>
> 6 An initializer-list is ill-formed if the number of initializer-clauses
> exceeds the number of members or elements to initialize.
>
> Hmm, GCC just segfaults. I wonder whether printing "segfault" is a valid
> runtime diagnostic message. Any insights?

This should be core issue

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#938

HTH & Greetings from Bremen,

Daniel Kr   gler


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]