|
Boost : |
From: Maxim Yegorushkin (maxim.yegorushkin_at_[hidden])
Date: 2006-06-15 16:50:39
Maxim Yegorushkin wrote:
> Ion Gaztañaga wrote:
>>> what is your basis for that assertion?
>> Because I wrote that header thinking (wrongly, because of my ADL
>> ignorance and VC support) that it would be used for ADL lookup:
>>
>> //////////////////////////////////////////////////////////////////////////////
>> //
>> // (C) Copyright Ion Gaztañaga 2005.
>> // Distributed under the Boost Software License, Version 1.0.
>> // (See accompanying file LICENSE_1_0.txt or copy at
>> // http://www.boost.org/LICENSE_1_0.txt)
>> //
>> //////////////////////////////////////////////////////////////////////////////
>>
>> #ifndef BOOST_POINTER_CAST_HPP
>> #define BOOST_POINTER_CAST_HPP
>>
>> namespace boost {
>>
>> //static_pointer_cast overload for raw pointers
>> template<class T, class U>
>> inline T* static_pointer_cast(U *ptr)
>> {
>> return static_cast<T*>(ptr);
>> }
>>
>> //..
>>
>> } // namespace boost
>>
>> #endif //BOOST_POINTER_CAST_HPP
>>
>> I wrote this because I wanted to use it though ADL. If
>> static_pointer_cast<Target>() can't be used through ADL, it's certainly
>> a very limited header. Unless I know that my pointer can be a raw
>> pointer, I know all the possible pointer types and I include all the
>> headers, this header is useless. Until Boost 1.34 this header didn't exist.
>>
>> But I still see a need for customizable cast functions. Since shared_ptr
>> is de-facto standard, I'm just proposing new generic cast functions.
>> What about template specialization?:
>>
>> //pointer_cast.hpp
>>
>> namespace boost {
>>
>> template<class TargetPtr>
>> class cast_to;
>>
>> template<class T>
>> class cast_to<T*>
>> {
>> template<class Source>
>> static T * using_static_cast(Source *source)
>> { return static_cast<T*>(source); }
>> }
>>
>> } //namespace boost {
>>
>>
>> namespace boost {
>>
>> //smart_ptr.hpp
>>
>> template<class T>
>> class cast_to<smart_ptr<T> >
>> {
>> template<class Source>
>> static smart_ptr<T>
>> using_static_cast(const smart_ptr<Source> &source);
>> }
>>
>> } //namespace boost {
>>
>> // pointer independent code
>>
>> BasePtr d_ptr = ...
>>
>> typedef typename
>> boost::pointer_to_other<BasePtr, Base>::type DerivedPtr;
>>
>> DerivedPtr target = cast_to<DerivedPtr>::using_static_cast(b_ptr);
>>
>> ////////////////////////////////
>>
>> Sorry if there are errors in the code. Any suggestion? Time to request
>> cast operator overloading for user classes?
>
> May it be simplified to the following?
[]
Please ignore the previous code. Kindly consider the following:
template<class T> struct type {};
// static_pointer_cast_impl overload for raw pointers
template<class T, class U>
inline T* static_pointer_cast_impl(U *ptr, type<T>)
{
return static_cast<T*>(ptr);
}
// static_pointer_cast ADL dispatcher function
template<class T, class U>
inline T static_pointer_cast(U u)
{
return static_pointer_cast_impl(u, type<T>()); // ADL kicks in here
}
// ...
template<class T> class smart_ptr { /* ... */ };
// static_pointer_cast_impl overload for raw smart_ptr
template<class T, class U>
inline smart_ptr<T> static_pointer_cast_impl(smart_ptr<U> const& p, type<T>)
{
return smart_ptr<T>(p.get());
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk