Re: [boost] FW: Suggestion for boost`s smart pointers

-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Howard Hinnant Sent: Friday, December 01, 2006 4:34 PM To: boost@lists.boost.org Subject: Re: [boost] FW: Suggestion for boost`s smart pointers
On Dec 1, 2006, at 5:00 PM, Sohail Somani wrote:
Most C libraries have make_X/free_X pairs. So a typical C call could look like:
... struct T * c = make_T(args); do_stuff_with_a_T(c); free_T(c); ...
Which I would like to replace with:
... typedef some_ptr<T,Deleter<free_T> > T_ptr; T_ptr c(make_T(args)); do_stuff_with_a_T(c.get()); ...
Does this work for you?
typedef unique_ptr<T, void (*)(void*)> T_ptr; int args; T_ptr c(make_T(args), std::free); // assumes make_T uses std::malloc do_stuff_with_a_T(c.get());
http://home.twcny.rr.com/hinnant/cpp_extensions/unique_ptr_03.html
I wouldn't really need the extra indirection that seems to be implied there but that's a step in the right direction.

On Dec 1, 2006, at 7:37 PM, Sohail Somani wrote:
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Howard Hinnant Sent: Friday, December 01, 2006 4:34 PM To: boost@lists.boost.org Subject: Re: [boost] FW: Suggestion for boost`s smart pointers
On Dec 1, 2006, at 5:00 PM, Sohail Somani wrote:
Most C libraries have make_X/free_X pairs. So a typical C call could look like:
... struct T * c = make_T(args); do_stuff_with_a_T(c); free_T(c); ...
Which I would like to replace with:
... typedef some_ptr<T,Deleter<free_T> > T_ptr; T_ptr c(make_T(args)); do_stuff_with_a_T(c.get()); ...
Does this work for you?
typedef unique_ptr<T, void (*)(void*)> T_ptr; int args; T_ptr c(make_T(args), std::free); // assumes make_T uses std::malloc do_stuff_with_a_T(c.get());
http://home.twcny.rr.com/hinnant/cpp_extensions/unique_ptr_03.html
I wouldn't really need the extra indirection that seems to be implied there but that's a step in the right direction.
<nod> Agreed. The indirection is in there really only because you said "C". The deleter type can easily be a C++ class type which can then do anything you want. -Howard
participants (2)
-
Howard Hinnant
-
Sohail Somani