Re: [Boost-users] Suggestion for boost`s smart pointers

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of David Klein
Sohail Somani wrote:
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of mark cox
wow, this seems like a great idea, a scoped_ptr with deleter would be v.useful. mark
I've been thinking something like (just something I hacked together just now). Not sure if there is a nicer way.
[snip] +1 vote for this idea :)
i actually kind of did this for my current project, because i needed a custom deleter for scoped_ptr, when shared_ptr was to much overhead...
As did I, but it was not for performance but for readability. Shared pointers mean... Shared. Scoped don't.

Sohail Somani wrote:
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of David Klein
Sohail Somani wrote:
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of mark cox
wow, this seems like a great idea, a scoped_ptr with deleter would be v.useful. mark
I've been thinking something like (just something I hacked
together just
now). Not sure if there is a nicer way.
[snip]
+1 vote for this idea :)
i actually kind of did this for my current project, because i needed a custom deleter for scoped_ptr, when shared_ptr was to much overhead...
As did I, but it was not for performance but for readability. Shared pointers mean... Shared. Scoped don't. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
yep exactly, thatswhy i "ripped" scoped_ptr out of boost and added my custom_deleter stuff. (i only *thought* about shared_ptr to do the job) so are there any objections about adding this feature to scoped_ptr? -- regards dave

David Klein wrote:
so are there any objections about adding this feature to scoped_ptr?
I'm sure I have seen this discussion before. I believe the issue was the size of scoped_ptr itself. It is supposed to be a lightweight object, no bigger than the pointer it holds. Any added overhead is unacceptable. If you can implement the feature without adding any data members, fine. Otherwise, please create a new class. -- Dick Hadsell 914-259-6320 Fax: 914-259-6499 Reply-to: hadsell@blueskystudios.com Blue Sky Studios http://www.blueskystudios.com 44 South Broadway, White Plains, NY 10601

On Nov 29, 2006, at 10:33 AM, Richard Hadsell wrote:
David Klein wrote:
so are there any objections about adding this feature to scoped_ptr?
I'm sure I have seen this discussion before. I believe the issue was the size of scoped_ptr itself. It is supposed to be a lightweight object, no bigger than the pointer it holds. Any added overhead is unacceptable. If you can implement the feature without adding any data members, fine. Otherwise, please create a new class.
<nod> This comes up now and then. Fwiw, anyone/everyone is welcome to my emulation of the proposed C+ +0X unique_ptr: http://home.twcny.rr.com/hinnant/cpp_extensions/unique_ptr_03.html Sorry, it isn't documented. Here's the best documentation I currently have: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/ n1856.html#Addition%20-%20Class%20template%20unqiue_ptr The (non)copyright at the top means you can do whatever you want with the code, including stripping my name out of it. In a nutshell: * It has a deleter. * If the deleter is an empty class, sizeof(unique_ptr<T, D>) == sizeof(T*). * It has unique ownership semantics like auto_ptr/scoped_ptr. * It does not transfer ownership with copy syntax (modulo a bug mentioned in the source code). * It has cute syntax to safely handle arrays: unique_ptr<T[]>. * You can also customize the storage type (defaults to T*) by giving your deleter an optional D::pointer type. * The deleter can be a reference to a deleter. The sizeof the unique_ptr swells by a word when you do this, but it does come in handy when you want to reference some other deleter instead of copy it in to the unique_ptr. * The deleter can be a function pointer. sizeof swells here too. * It has been proposed for standardization. -Howard

Howard Hinnant wrote:
* It has cute syntax to safely handle arrays: unique_ptr<T[]>. * You can also customize the storage type (defaults to T*) by giving your deleter an optional D::pointer type.
Do you have a rationale/use cases for D::pointer and unique_ptr<T[N]>?

On Nov 29, 2006, at 11:07 AM, Peter Dimov wrote:
Howard Hinnant wrote:
* It has cute syntax to safely handle arrays: unique_ptr<T[]>. * You can also customize the storage type (defaults to T*) by giving your deleter an optional D::pointer type.
Do you have a rationale/use cases for D::pointer and unique_ptr<T[N]>?
Not written up. But D::pointer was put in in an attempt to support shared memory. In this use case you really need a smart pointer instead of a T*. This use case hasn't been tested with actual code. However the feature is about as free as it gets. It consumes no code size or run time. And the author of D can completely ignore the pointer type and not even provide it. unique_ptr searches D, and if pointer isn't found, uses T*. Put this one in the "might be useful, costs nothing to provide" category. On unique_ptr<T[N]> I'm assuming you're specifically talking about providing the N, as opposed to just unique_ptr<T[]>. If you're talking about array support in general, let me know and I'll address that. The motivation for unique_ptr<T[N]> is so that vendors can offer range checking on operator[](size_t i) (perhaps only in debug builds). The N is a compile time constant, so takes up no storage space. The main cost is potential code bloat as you'll get a different instantiation for every different N. But if you don't use unique_ptr<T[N]> (but instead use only unique_ptr<T[]>) then you don't pay for this feature. Oh, and though most deleters don't need to know the size of the allocation, some might. So unique_ptr<T[N], D> calls D(pointer, N). The default D ignores the N. -Howard

Howard Hinnant wrote:
The motivation for unique_ptr<T[N]> is so that vendors can offer range checking on operator[](size_t i) (perhaps only in debug builds). The N is a compile time constant, so takes up no storage space. The main cost is potential code bloat as you'll get a different instantiation for every different N. But if you don't use unique_ptr<T[N]> (but instead use only unique_ptr<T[]>) then you don't pay for this feature.
Oh, and though most deleters don't need to know the size of the allocation, some might. So unique_ptr<T[N], D> calls D(pointer, N). The default D ignores the N.
Thanks. This is somewhat implementation-oriented, though. I was trying to imagine use cases for T[N]. In what situations I would use it? A fixed-size buffer on the heap... it can make sense at times, but it's hard to squeeze it between array<>, vector<> and unique_ptr<T[]>. It's essentially syntactic sugar for unique_ptr< array<T,N> >, except it (or the result of dereferencing it) can't be used as a generic container. Then again, T[N] cannot either. I'm not interested in how free it is, just in how useful it is. :-) One could probably advance the argument that since we support T[], we might as well support T[N]. But the incompatible deleter interface makes it hard to transition from T[] to T[N] and back as code evolves. OTOH, one could just provide both overloads in the deleter. If it isn't a function pointer. <shakes head> Where was I?

On Nov 29, 2006, at 12:36 PM, Peter Dimov wrote:
I'm not interested in how free it is, just in how useful it is. :-)
<nod> If I were to scale usefulness as: 1. Would never use it. 2. ... 3. Might use it once in a while. 4. ... 5. I'd find it handy sometimes. 6. ... 7. ... 8. Lots of uses for it. 9. ... 10. Have to have it. It might go something like: unique_ptr<T> : 10 unique_ptr<T[]> : 5 unique_ptr<T[N]>: 3 If it costs me a bundle to support a 3 or a 5, I wouldn't even consider them. But these have very low costs in terms of: * Conceptual complexity (simple, intuitive interface) * Run time (none if not used, cheap if used) * Code size (none if not used, cheap if used) * Compile time (extremely little) * Implementation support (little) (those are listed in order of importance, imho of course) -Howard

Richard Hadsell <hadsell <at> blueskystudios.com> writes:
David Klein wrote: [...] If you can implement the feature without adding any data members, fine. Otherwise, please create a new class.
I agree too! Why not calling it raii_ptr?
participants (6)
-
David Klein
-
Howard Hinnant
-
Maitre Bart
-
Peter Dimov
-
Richard Hadsell
-
Sohail Somani