public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* ISO C++ forbids initialization in array new?
@ 2005-08-19  3:39 WU Yongwei
  2005-08-19  8:20 ` Jonathan Wakely
  2005-08-19 10:20 ` Alisdair Meredith
  0 siblings, 2 replies; 7+ messages in thread
From: WU Yongwei @ 2005-08-19  3:39 UTC (permalink / raw)
  To: gcc

Well, I see this in the gcc error message.  Can someone here kindly
point to me which part of the Standard specified this behaviour?  I
thought it should be in 5.3.4, but was not able to find the words
there.

By the way, anyone knows the rationale of this behaviour?

Thanks in advance.

Best regards,

Yongwei

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

* Re: ISO C++ forbids initialization in array new?
  2005-08-19  3:39 ISO C++ forbids initialization in array new? WU Yongwei
@ 2005-08-19  8:20 ` Jonathan Wakely
  2005-08-19  8:27   ` Jonathan Wakely
  2005-08-19 16:31   ` Joe Buck
  2005-08-19 10:20 ` Alisdair Meredith
  1 sibling, 2 replies; 7+ messages in thread
From: Jonathan Wakely @ 2005-08-19  8:20 UTC (permalink / raw)
  To: WU Yongwei; +Cc: gcc

WU Yongwei wrote:

> Well, I see this in the gcc error message.  Can someone here kindly
> point to me which part of the Standard specified this behaviour?  I
> thought it should be in 5.3.4, but was not able to find the words
> there.

It might be better if the error message said "non-default
initialization" since default initialization is allowed (and required).

I assume you are trying something like this:

    int* i = new int[5](23);


A new initializer must have the form (...) (see 5.3.4/1) and the only
way that can be used to initialize an array is for default construction.


This is OK, and default initialises the array, which default initialises
each element (8.5/5)

    int* i = new int[5]();

but this is not OK:

    int* i = new int[5](23);

because it is not valid to initialise an array like this:

    typedef int (five_ints)[5];
    five_ints i(23);

this gives:

array_init.cc:8: error: cannot initialize arrays using this syntax

HTH

jon

-- 
sigfault

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

* Re: ISO C++ forbids initialization in array new?
  2005-08-19  8:20 ` Jonathan Wakely
@ 2005-08-19  8:27   ` Jonathan Wakely
  2005-08-19 16:31   ` Joe Buck
  1 sibling, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2005-08-19  8:27 UTC (permalink / raw)
  To: WU Yongwei; +Cc: gcc

Jonathan Wakely wrote:

> WU Yongwei wrote:
> 
> > Well, I see this in the gcc error message.  Can someone here kindly
> > point to me which part of the Standard specified this behaviour?  I
> > thought it should be in 5.3.4, but was not able to find the words
> > there.
> 
> It might be better if the error message said "non-default
> initialization" since default initialization is allowed (and required).

Oops! that was meant to say "required for some types" (e.g.
const-qualified types, non-POD types)

Too early in the morning for me to be answering questions like this!

jon


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

* Re: ISO C++ forbids initialization in array new?
  2005-08-19  3:39 ISO C++ forbids initialization in array new? WU Yongwei
  2005-08-19  8:20 ` Jonathan Wakely
@ 2005-08-19 10:20 ` Alisdair Meredith
  2005-08-22  8:11   ` WU Yongwei
  1 sibling, 1 reply; 7+ messages in thread
From: Alisdair Meredith @ 2005-08-19 10:20 UTC (permalink / raw)
  To: gcc

WU Yongwei wrote:

> Well, I see this in the gcc error message.  Can someone here kindly
> point to me which part of the Standard specified this behaviour?  I
> thought it should be in 5.3.4, but was not able to find the words
> there.
> 
> By the way, anyone knows the rationale of this behaviour?

It is not explicitly forbidden. Rather, there is no syntax defined that
would enable it (so it is implicitly forbidden)

you might be interested in the following paper from the last ISO
mailing ;¬)

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1824.htm


AlisdairM

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

* Re: ISO C++ forbids initialization in array new?
  2005-08-19  8:20 ` Jonathan Wakely
  2005-08-19  8:27   ` Jonathan Wakely
@ 2005-08-19 16:31   ` Joe Buck
  2005-08-19 16:48     ` Andreas Schwab
  1 sibling, 1 reply; 7+ messages in thread
From: Joe Buck @ 2005-08-19 16:31 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: WU Yongwei, gcc

On Fri, Aug 19, 2005 at 09:19:55AM +0100, Jonathan Wakely wrote:
> WU Yongwei wrote:
> 
> > Well, I see this in the gcc error message.  Can someone here kindly
> > point to me which part of the Standard specified this behaviour?  I
> > thought it should be in 5.3.4, but was not able to find the words
> > there.
> 
> It might be better if the error message said "non-default
> initialization" since default initialization is allowed (and required).
> 
> I assume you are trying something like this:
> 
>     int* i = new int[5](23);

While this is not gcc-help, maybe we could make a FAQ item for this.
For people who want to do something like this, I suggest

    std::vector<int> i(5, 23);

If the reason vector wasn't used in the first place was because some
API needs an array, then use

    std::vector<int> vector_i(5, 23);
    int* i = &*vector.begin();

The reason for the &* is to convert the vector iterator into a
pointer to the first element.

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

* Re: ISO C++ forbids initialization in array new?
  2005-08-19 16:31   ` Joe Buck
@ 2005-08-19 16:48     ` Andreas Schwab
  0 siblings, 0 replies; 7+ messages in thread
From: Andreas Schwab @ 2005-08-19 16:48 UTC (permalink / raw)
  To: Joe Buck; +Cc: Jonathan Wakely, WU Yongwei, gcc

Joe Buck <Joe.Buck@synopsys.COM> writes:

> On Fri, Aug 19, 2005 at 09:19:55AM +0100, Jonathan Wakely wrote:
>> I assume you are trying something like this:
>> 
>>     int* i = new int[5](23);
>
> While this is not gcc-help, maybe we could make a FAQ item for this.
> For people who want to do something like this, I suggest
>
>     std::vector<int> i(5, 23);
>
> If the reason vector wasn't used in the first place was because some
> API needs an array, then use
>
>     std::vector<int> vector_i(5, 23);
>     int* i = &*vector.begin();

This is not quite the same, because when vector_i goes out of scope it
will be destructed, so you can't use the pointer beyond that point.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: ISO C++ forbids initialization in array new?
  2005-08-19 10:20 ` Alisdair Meredith
@ 2005-08-22  8:11   ` WU Yongwei
  0 siblings, 0 replies; 7+ messages in thread
From: WU Yongwei @ 2005-08-22  8:11 UTC (permalink / raw)
  To: gcc

On 8/19/05, Jonathan Wakely <cow@compsoc.man.ac.uk> wrote:
> WU Yongwei wrote:
> > Well, I see this in the gcc error message.  Can someone here kindly
> > point to me which part of the Standard specified this behaviour?  I
> > thought it should be in 5.3.4, but was not able to find the words
> > there.
[snipped]
> This is OK, and default initialises the array, which default initialises
> each element (8.5/5)
> 
>    int* i = new int[5]();
> 
> but this is not OK:
> 
>    int* i = new int[5](23);
> 
> because it is not valid to initialise an array like this:
> 
>    typedef int (five_ints)[5];
>    five_ints i(23);
> 
> this gives:
> 
> array_init.cc:8: error: cannot initialize arrays using this syntax

This sounds reasonable.  The only problem is that it does not
constitute proof.  It is complete OK if the standard disallowed `int
a[5](23)' while allowing `new int[5](23)', just as older GCC did.

On 8/19/05, Alisdair Meredith <alisdair.meredith@uk.renaultf1.com> wrote:
> WU Yongwei wrote:
> 
> > Well, I see this in the gcc error message.  Can someone here kindly
> > point to me which part of the Standard specified this behaviour?  I
> > thought it should be in 5.3.4, but was not able to find the words
> > there.
> >
> > By the way, anyone knows the rationale of this behaviour?
> 
> It is not explicitly forbidden. Rather, there is no syntax defined that
> would enable it (so it is implicitly forbidden)

My observations.  According to 5.3.4, an expression like `new
int[5](23)' is well-formed.  Just that no semantics are formally
defined for it.

The behaviour might be OK, but the message seems a little misleading. 
Would something like `cannot specify initializer for arrays' be better
since it really has little to do with `array new'?

Also, the trends of gcc removing existing `extensions' are a little
worrisome.  IMHO, the extension should be allowed and a warning in the
case of `-W' or `-ansi' should be issued.

Best regards,

Yongwei

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

end of thread, other threads:[~2005-08-22  8:11 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-19  3:39 ISO C++ forbids initialization in array new? WU Yongwei
2005-08-19  8:20 ` Jonathan Wakely
2005-08-19  8:27   ` Jonathan Wakely
2005-08-19 16:31   ` Joe Buck
2005-08-19 16:48     ` Andreas Schwab
2005-08-19 10:20 ` Alisdair Meredith
2005-08-22  8:11   ` WU Yongwei

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).