Boost logo

Boost :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2006-06-15 15:04:03


> 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?

Regards,

Ion


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk