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)
Change History (5)
by , 12 years ago
Attachment: | extra_scoped_array_constructor.patch added |
---|
comment:1 by , 11 years ago
follow-up: 3 comment:2 by , 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...
comment:3 by , 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 , 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
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.