Modify

Opened 7 years ago

Last modified 6 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 7 years ago.

Download all attachments as: .zip

Change History (5)

Changed 7 years ago by Niels Dekker

comment:1 Changed 6 years ago by olafvdspek@…

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 Changed 6 years ago by Niels Dekker

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

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

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 Changed 6 years ago by Niels Dekker

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

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The owner will remain Peter Dimov.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.