Boost logo

Boost :

From: Larry Evans (cppljevans_at_[hidden])
Date: 2004-10-18 17:55:42


On 10/18/2004 02:18 PM, Oleg Fedtchenko wrote:
[snip]
> I also did not compile this piece of code.

The syntax was wrong. It should be:

     template<class Y>
     shared_ptr(shared_ptr<Y> const & r, T Y::*m)
     : px(&(r.get()->*m)), pn(r.pn)
     {
     }

which does compile.

> But if I caught the idea behind it right, then to place constrains on
> acceptable combinations *member-owner* looks surprising.
>
> An owner knows all of it members exposed through shared_ptr.
> And it contains all of those acceptable combinations
> *type of owner and its corresponding acceptable type of member*.
>
> But I had a task of hierarchy of several classes two of which were
> used as owners for the same type of member exposed through a
> smart pointer. One of those owners had a pointer to another one.
> If I had used a CTOR with both pointers to a member and its owner
> (as at the beginning)
> I could have passed in the pointer to another (wrong) owner
> instead of *this*. And the type *member-owner* constrains above
> would have not prevented an access violation.
>
> But member_ptr keeping a pointer to an owner in its base class
> does not allow to make a mistake.
I'm having trouble understanding. Could you provide example code,
or indicate whether the following is even close to your meaning:

struct member_type{};

enum owner_id{ id1, id2};

template<owner_id Id>
struct other_id
;

template<>
struct other_id<id1>
{
     static owner_id const the_id=id2;
};

template<>
struct other_id<id2>
{
     static owner_id const the_id=id1;
};

using namespace boost;

template<owner_id Id>
struct an_owner
: public other_id<Id>
{
     typedef other_id<Id> other_type;
     typedef an_owner<other_type::the_id> other_owner_type;
     member_type a_member;
     shared_ptr<member_type> p_member;
     an_owner(void){}
     an_owner(shared_ptr<other_owner_type>const & a_other)
     : p_member(a_other, &other_owner_type::a_member)
     {}
};

int main()
{

     {
         shared_ptr<an_owner<id1> > p1(new an_owner<id1>);
         shared_ptr<an_owner<id2> > p2(new an_owner<id2>(p1));
     }

     return boost::report_errors();
}


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