Topic: Non-default constructor with array-new -- why not?


Author: vytautas.shaltenis@gmail.com (Vytautas Shaltenis)
Date: Thu, 17 Feb 2005 18:26:46 GMT
Raw View
"msalters" <Michiel.Salters@logicacmg.com> wrote in message news:<1108472528.661138.23690@f14g2000cwb.googlegroups.com>...
> "Trevor L. Jackson, III" wrote:
> > John Nagle wrote:
> >
> > > Vytautas Shaltenis wrote:
> > >
> > >> Hi
> > >>
> > >>   I am wondering why C++ standard forbids calling non-default
> > >> constructor when creating array of objects with array-new.
> > >>  I can't think of any good reason for disallowing that. I've
> > >>  been looking for an answer in many places, but couldn't
> > >> find a slightest hint at the reason.
> > >
> > >    Where would the arguments for each array member be written?
> >
> > One set of arguments for each element, arguments provided at the site
> of
> > the new call.  Or (shades of mapcar), by an array of arguments, said
> > argument array at least as long as the new'd array.
>
> At which point a std::vector<T>( Iter begin, Iter end ) suddenly looks
> like a 99% solution.

  That one percent is a really huge one, I'd say :-)

  The point with object initialization is that after a call to constructor
one should have a fully constructed, functional object. It is often
impossible to create a functional object without supplying it with some
external information. Whenever one has such a case, his object is malformed
if it was initialized with the default constructor. Hence, a good practice
would be to declare default constructor private and have a comment stating
that this was done on purpose.

  std::vector is fine in this respect, it doesn't require default constructor
to be available. That's why it manages to reach those 99% :-)

  OTOH, when that 1% is the case, the lack of the construct being discussed
means that it's *impossible* to create an array of such objects at all!
(Yes, there are workarounds, but workarounds are not the best ways to go,
are they?).

  Now, I'm not bitching about the language lacking that feature. I can live
without it. I'm just curious, I want to know /why/ this feature was excluded
from (or was not considered to get into?) the standard. Whenever I encounter
something like that, by default I assume that the committee guys had some
very good (most probably technical) reason to reject the feature. This case
is not an exception. The only difference is that in most cases I can google
the answer in 15 minutes and don't need to raise the devil in comp.std.c++ :-)

--
Vytautas Shaltenis
On the Web: http://www.mif.vu.lt/~vysa1570

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: vytautas.shaltenis@gmail.com (Vytautas Shaltenis)
Date: Sun, 13 Feb 2005 21:23:38 GMT
Raw View
Hi

  I am wondering why C++ standard forbids calling non-default constructor when
creating array of objects with array-new. I can't think of any good reason for
disallowing that. I've been looking for an answer in many places, but couldn't
find a slightest hint at the reason.

  Could somebody please shed some light on it?

  Thanks,
  Vytautas

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: nagle@animats.com (John Nagle)
Date: Mon, 14 Feb 2005 06:12:48 GMT
Raw View
Vytautas Shaltenis wrote:

> Hi
>
>   I am wondering why C++ standard forbids calling non-default constructor when
> creating array of objects with array-new. I can't think of any good reason for
> disallowing that. I've been looking for an answer in many places, but couldn't
> find a slightest hint at the reason.
>
>   Could somebody please shed some light on it?
>

    Where would the arguments for each array member be written?

    John Nagle

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Mon, 14 Feb 2005 18:16:43 GMT
Raw View
In article <421036A1.1040609@animats.com>, John Nagle
<nagle@animats.com> writes
>   Where would the arguments for each array member be written?

Try a variant syntax that could work:

mytype * a_ptr = new int[20]{{1}, {2, 2.1}, {3, "help"}, {4}};

to create an array of 20 and initialise the elements with: mytype(int),
mytype(int, double), mytype(int, std::string), mytype(int) and mytype()
for the remaining 16 elements.

Actually, IMO, the whole issue of intialiser syntax in C++ needs
cleaning up.


--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: vytautas.shaltenis@gmail.com (Vytautas Shaltenis)
Date: Mon, 14 Feb 2005 21:24:39 GMT
Raw View
nagle@animats.com (John Nagle) wrote in message news:<421036A1.1040609@animats.com>...
>
>     Where would the arguments for each array member be written?

  Same constructor with same argument for each element. Like this:

  Foo* arrayOfFoo = new Foo [10] (666);

  For each element Foo::Foo (int) would be called. GCC supports this as an
extension, so it's obviously technically possible. And seems reasonable to me.

--
Vytautas Shaltenis
On the Web: http://www.mif.vu.lt/~vysa1570

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: tlj3@comcast.net ("Trevor L. Jackson, III")
Date: Tue, 15 Feb 2005 00:18:25 GMT
Raw View
John Nagle wrote:

> Vytautas Shaltenis wrote:
>
>> Hi
>>
>>   I am wondering why C++ standard forbids calling non-default
>> constructor when
>> creating array of objects with array-new. I can't think of any good
>> reason for
>> disallowing that. I've been looking for an answer in many places, but
>> couldn't
>> find a slightest hint at the reason.
>>
>>   Could somebody please shed some light on it?
>>
>
>    Where would the arguments for each array member be written?

One set of arguments for each element, arguments provided at the site of
the new call.  Or (shades of mapcar), by an array of arguments, said
argument array at least as long as the new'd array.

/tj3

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "msalters" <Michiel.Salters@logicacmg.com>
Date: Tue, 15 Feb 2005 09:48:21 CST
Raw View
"Trevor L. Jackson, III" wrote:
> John Nagle wrote:
>
> > Vytautas Shaltenis wrote:
> >
> >> Hi
> >>
> >>   I am wondering why C++ standard forbids calling non-default
> >> constructor when creating array of objects with array-new.
> >>  I can't think of any good reason for disallowing that. I've
> >>  been looking for an answer in many places, but couldn't
> >> find a slightest hint at the reason.
> >
> >    Where would the arguments for each array member be written?
>
> One set of arguments for each element, arguments provided at the site
of
> the new call.  Or (shades of mapcar), by an array of arguments, said
> argument array at least as long as the new'd array.

At which point a std::vector<T>( Iter begin, Iter end ) suddenly looks
like a 99% solution.

Don't add fancy capabilities to a low-level building block.

Regards,
Michiel Salters

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: tlj3@comcast.net ("Trevor L. Jackson, III")
Date: Wed, 16 Feb 2005 18:54:25 GMT
Raw View
msalters wrote:

> "Trevor L. Jackson, III" wrote:
>
>>John Nagle wrote:
>>
>>
>>>Vytautas Shaltenis wrote:
>>>
>>>
>>>>Hi
>>>>
>>>>  I am wondering why C++ standard forbids calling non-default
>>>>constructor when creating array of objects with array-new.
>>>> I can't think of any good reason for disallowing that. I've
>>>> been looking for an answer in many places, but couldn't
>>>>find a slightest hint at the reason.
>>>
>>>   Where would the arguments for each array member be written?
>>
>>One set of arguments for each element, arguments provided at the site
>
> of
>
>>the new call.  Or (shades of mapcar), by an array of arguments, said
>>argument array at least as long as the new'd array.
>
>
> At which point a std::vector<T>( Iter begin, Iter end ) suddenly looks
> like a 99% solution.
>
> Don't add fancy capabilities to a low-level building block.

Providing each element with its index is not a fancy capability.  It is
an elemental operation upon which customized initialization can be
built.  The language has a serious weakness in the inadequacy of
initialization in all forms.  There's no reason to resist improving that.

/tj3

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]