Re: Different syntax for shared arrays between unique_ptr and
shared_ptr
Am 15.04.2012 08:29, schrieb Edward Diener:
As I understand it specifying a non-shared array with unique_ptr is
done using the syntax:
std::unique_ptr<T[]> up(new T[n]);
The unique_ptr has a partial specialization to handle this.
This is so, because std::unique_ptr is supposed to be the direct
replacement for std::auto_ptr or a naked pointer and there should be
near to zero runtime drawback when used in stateless standard form (no
deleter or just an empty, stateless deleter). This means, basically all
aspects are determined during compile-time. Because of this it seemed
natural to also add a specialization for arrays, because such a
specialization also has to take care for subtle gotchas related to
conversions (Arrays are not polymorphic).
The syntax for handling shared arrays with shared_ptr is different:
std::shared_ptr<T> sp(new T[n],std::default_delete<T[]>());
The emphasis of std::shared_ptr was heavily inspired by dynamic
(runtime) properties, especially it was considered important that
instances of such types can be exchanged between dynamic libraries (Even
though not "de jure" regulated by the standard, it is a de facto
reality). This mean that type-erasing the deleter was a natural design
decision.
Why is the syntax for working with arrays in unique_ptr and shared_ptr
different ?
There was indeed a related request by US comment 105, see
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3289.pdf
and here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3296.html#US105
The request was rejected, because no clear consensus could be found:
"There is no consensus to adopt this change"
This means that even though some arguments where brought up that
demonstrate the inconsistency the arguments were not overwhelming
convincing at that point.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]