Boost logo

Boost :

From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2002-07-30 15:04:31


> class X { int x; };
> class Y { int y; };
> class Derived: public X, public Y {};

[...]

> A plain old Y object, we have a ptr<Y>, containing some pointer P. One
> reasonable implementation is:
> +----------------
> | Ref count
> P -> +----------------
> | Y::y
> +----------------
>
> Now, for a Derived object, which we now make a ptr<Y> for. Where to
> place the pointer P for this, A or B?
> +----------------
> | Ref count
> A -> +----------------
> | X::x
> B -> +----------------
> | Y::y
> +----------------
>

[...]

> If you claim you have a solution that keeps sizeof(ptr<X>) == sizeof(X
> *), you'll have to demonstrate it more convincingly.

What about a second template parameter which will restrict operator new ()
allocations:

template <typename _T, class _C = _T>
    struct ptr
    {
        template <typename _U>
            ptr(_U * a_p) : m_ptr(a_p)
            {
                // typeid(_C) == typeid(_U) compile-time assertion here
            }
    };

ptr<Derived> pD = new Derived;
ptr<Derived, X> pX = new X;
ptr<Derived, Y> pY = new Y;

pX = pD; // Won't work
pY = pD; // Won't work

... or maybe there is another compile-time way to assert the pointer is
always at the same offset also ...

Philippe A. Bouchard


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