Topic: Array initialization


Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/08/12
Raw View
On 11 Aug 99 06:23:43 GMT, Steve <shockema@REMOVETHIS.cs.indiana.edu> wrote:

>    // Initialize ("construct") every element with a value of 5.
>    int * pArray = new int [10] (5);

Not standard C++.
Compile with "--pedantic" to enforce standard compatibility.
IMHO, "--pedantic" should be the default and there should be a
"--extensions" to turn on extensions.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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: "Steve" <shockema@REMOVE THIS.cs.indiana.edu>
Date: 1999/08/11
Raw View
Is the following code legal in the standard
(g++ supports this but VC++ does not):

int main()
{
    // Create an array of 10 integers
    // Initialize ("construct") every element with a value of 5.
    int * pArray = new int [10] (5);
    return 0;
}

If so, how flexible is this? Can this notation be used to initalize arrays
of objects
using constructors with multiple parameters?

    Thanks in advance...
        Steve
---
[ 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: wmm@fastdial.net
Date: 1999/08/11
Raw View
In article <h9%r3.936$e4.1937@news1.iquest.net>,
  "Steve" <shockema@REMOVE THIS.cs.indiana.edu> wrote:
> Is the following code legal in the standard
> (g++ supports this but VC++ does not):
>
> int main()
> {
>     // Create an array of 10 integers
>     // Initialize ("construct") every element with a value of 5.
>     int * pArray = new int [10] (5);

No, it's not.  5.3.4p15 lists all the possible initializations for
new-expressions, and the only two that apply to arrays are either
with the initializer omitted or specified as "()", both of which
do default-initialization.  The case where the parentheses
contain a non-empty expression-list is not permitted when the type
being created is an array type.

--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


[ 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: khan@xraylith.wisc.edu (Mumit Khan)
Date: 1999/08/11
Raw View
In article <h9%r3.936$e4.1937@news1.iquest.net>,
Steve <shockema@REMOVE THIS.cs.indiana.edu> wrote:
>Is the following code legal in the standard

No. See [expr.new]/15 for valid new-expression.

>(g++ supports this but VC++ does not):

>
>int main()
>{
>    // Create an array of 10 integers
>    // Initialize ("construct") every element with a value of 5.
>    int * pArray = new int [10] (5);
>    return 0;
>}

It's a GNU extension. FYI, most compilers have some set of options to
check if a piece of code is using compiler-specific extensions or not.

If you're using GCC 2.95 for example:

  $ gcc -c -ansi -pedantic-errors foo.cc

I'm sure VC++ has something similar.

>If so, how flexible is this? Can this notation be used to initalize arrays
>of objects
>using constructors with multiple parameters?

This is non-portable and non-standard, so unless you only use compilers
that support this extension, there is no flexibility.

Regards,
Mumit



[ 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              ]