|
Boost : |
From: Tomasz Kowalczyk (tomek_at_[hidden])
Date: 2000-07-28 07:46:18
David Abrahams wrote:
>
> Unfortunately, the solution proposed below is unacceptable, because it would
> allow construction of shared_ptr<X> from shared_ptr<Y> even where X and Y
> are unrelated types. That code should only compile where X and Y are the
> same type or where Y is derived from X.
>
There was a thread about this issue in clc++m named "Guru of the Week
#71: Inheritance Traits?". Solutions given there, in conjunction with
compile time asserts should solve class relationship problem.
Here is example code which checks whether T inherits from From:
template <class From>
struct derives_helper {
typedef derives_helper inherited;
typedef char yes[1];
typedef char no[2];
static yes &check_( From * );
static no &check_( ... );
};
template <class T, class From>
struct derives
: protected derives_helper<From>
{
enum {
value = sizeof( inherited::check_((T*)0) ) == sizeof(yes)
};
};
Nothing special. Even msvc should handle this.
Cheers
Tomasz
> -Dave
>
> ----- Original Message -----
> From: "Gernot Neppert" <gn_at_[hidden]>
> To: <boost_at_[hidden]>
> Sent: Thursday, July 27, 2000 10:05 AM
> Subject: [boost] shared_ptr implementation without template friends
>
> > Hi!
> > I've been using the template class "shared_ptr" with a compiler that
> doesn't
> > support template friend declarations (You will have guessed already: It's
> > Microsoft VC++ 6).
> > So, the symbol BOOST_NO_MEMBER_TEMPLATE_FRIENDS is defined, and everything
> > works fine - except that the members of the class become publicly exposed.
> > This is really annoying, so I came up with a proposal. Although it
> > introduces an ugly reinterpret_cast, this is confined to two methods
> > (templated ctor and assignment operator), whereas the additional "public:"
> > affected the entire class. Here's the code for the ctor:
> >
> > #if defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS )
> > template<typename Y>
> > shared_ptr(const shared_ptr<Y>& r) throw() : px(r.get()) {
> > ++*(pn = reinterpret_cast<const shared_ptr<T>&>( r ).pn);
> > }
> > #else // original version
> >
> > template<typename Y>
> > shared_ptr(const shared_ptr<Y>& r) throw() : px(r.px) {
> > ++*(pn = r.pn);
> > }
> >
> > #endif
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk