Question and idea for smart pointers

Hello to all, While developing the Boost Shmem library I needed to be independent of the pointer types (raw or smart), so I started looking for those functions in Boost. As I've seen an overloaded "get_pointer" function returns a raw pointer from a generic pointer, and I've made the same with the relative offset_ptr from Boost Shmem. But to treat all types of pointer generically I needed to obtain from a generic pointer a pointer to other type of the same class. I've solved this defining in my smart pointer an internal type following the STL allocator rebind mechanism: /*!Obtains offset_ptr <OtherType> from offset_ptr*/ template<typename OtherType> struct pointer_to_type { typedef my_smart_ptr<OtherType> type; }; and the structs: template <typename Ptr, typename NewValueType> struct pointer_to_other { typedef typename Ptr::template pointer_to_type<NewValueType>::type type; }; template <typename T, typename NewValueType> struct pointer_to_other<T*, NewValueType> { typedef NewValueType *type; }; so that the user can generically write: typedef pointer_to_other<NewType>(my_smart_ptr)::type new_pointer_t; and obtain a pointer of the same class (smart pointer or raw pointer) to other type. My question is, do you find this mechanism useful to include it in boost in a general way, and if so, is there another way of doing this without modifying existing classes (for example, if I want use these mechanism with shared_ptr)? Regards, /Ion

Ion Gaztañaga wrote: [...]
template< class P, class V > struct pointer_to_other; template< template<class> class P, class U, class V > struct pointer_to_other< P<U>, V > { typedef P<V> type; }; #include <boost/shared_ptr.hpp> #include <typeinfo> #include <iostream> int main() { typedef pointer_to_other< boost::shared_ptr<int>, double >::type pointer; std::cout << typeid(pointer).name() << std::endl; } On compilers that can't handle the above, just add partial specializations for "well known" pointer types (auto_ptr, shared_ptr).

On compilers that can't handle the above, just add partial specializations for "well known" pointer types (auto_ptr, shared_ptr).
I think this would work only with template classes with only one template parameter, like shared_ptr, but not with other smart pointers (offset_ptr or others, or future smart pointers that have some extra policy template parameters). I would propose adding a similar general well-known (at least with boost smart pointers) mechanism to obtain a pointer to other generically. Do you think this pointer_to_other is general enough to include it in the same level as get_pointer, or do you find this mechanism too particular? Regards, Ion

On Fri, Jan 07, 2005 at 11:30:34AM +0100, Ion Gazta?aga wrote:
Then you can add other partial specialisations: template< template<class,class> class P, class U, class V, class W > struct pointer_to_other< P<U,W>, V > { typedef P<V,W> type; }; This will work with all two-parameter smart pointer templates without having to change them. If you need it for smart pointer templates that take three parameters, add another specialisation. jon -- This space left intentionally blank.
participants (3)
-
Ion Gaztañaga
-
Jonathan Wakely
-
Peter Dimov