Opened 12 years ago

Last modified 11 years ago

#4637 new Feature Requests

An extra scoped_array(n) constructor, please!

Reported by: niels_dekker Owned by: Peter Dimov
Milestone: To Be Determined Component: smart_ptr
Version: Boost 1.44.0 Severity: Problem
Keywords: Cc:

Description

When I construct a non-empty scoped_array<my_type>, I currently need to specify the type my_type twice:

  boost::scoped_array<my_type> a( new my_type[n]() );

Please consider supporting the following as an equivalent:

  boost::scoped_array<my_type> a(n);

A patch to the trunk (scoped_array.hpp + smart_ptr_test.cpp) is hereby attached.

Attachments (1)

extra_scoped_array_constructor.patch (1.4 KB ) - added by niels_dekker 12 years ago.

Download all attachments as: .zip

Change History (5)

by niels_dekker, 12 years ago

comment:1 by olafvdspek@…, 11 years ago

Hoi Niels,

You used "new T[]()" instead of "new T[]". Do you require the former or would the latter be fine for you? Not everyone needs the elements to be initialized. Initializing them after construction is possible, uninitializing them isn't.

comment:2 by niels_dekker, 11 years ago

Hoi Olaf,

Thanks for your feedback. I prefer to have the elements value-initialized by such a scoped_array(n) constructor. That's also consistent with the interface of std::vector:

   // All 'n' elements value-initialized:
   boost::scoped_array<my_type> a(n);
   std::vector<my_type> v(n);

People who really need to have uninitialized elements can still use the old (verbose) syntax, of course...

in reply to:  2 comment:3 by Olaf van der Spek <olafvdspek@…>, 11 years ago

Replying to niels_dekker:

Hoi Olaf,

Thanks for your feedback. I prefer to have the elements value-initialized by such a scoped_array(n) constructor.

The difference only applies to types without constructors (AFAIK).

That's also consistent with the interface of std::vector:

   // All 'n' elements value-initialized:
   boost::scoped_array<my_type> a(n);
   std::vector<my_type> v(n);

People who really need to have uninitialized elements can still use the old (verbose) syntax, of course...

That argument applies to not initializing as well. I can't remember ever using new T[]() actually. What use cases do you have for it?

comment:4 by niels_dekker, 11 years ago

For the record, this subject is being discussed at [boost] [smart_ptr] scoped_array / shared_array (size_t) constructor, starting at http://lists.boost.org/Archives/boost/2011/10/187198.php

Note: See TracTickets for help on using tickets.