|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2000-07-28 08:32:28
Okay, you could use all this mechanism with a compile-time assert to make
sure it wouldn't compile unless Y were derived from X, but the proposed
approach causes undefined behavior anyway because of the reinterpret-cast
(try it with multiple-inheritance and see).
-Dave
----- Original Message -----
From: "Tomasz Kowalczyk" <tomek_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, July 28, 2000 8:46 AM
Subject: Re: [boost] shared_ptr implementation without template friends
> 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