Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-05-29 22:04:16


Ross Smith wrote:

> That would have helped, yes, but it wasn't the main problem. Consider a
> slightly elaborated version of the above:
>
> struct Y;
> struct X {
> Y* y;
> ~X() { delete y; } // ***
> };
> struct Y {
> X* x;
> ~Y() { delete x; }
> };
>
> That won't work, of course; the compiler rejects the line marked ***.

        Yes. But these are hand coded classes, and you can ..

> It will work if you put the body of ~X() out of line.

        But that solution is NOT available for templates.
        
> Your original
> example with smart pointers was exactly analogous to this, and I see no
> reason to expect it to work.

        I do. The reason is that there is no other solution.
In the above example, lookup occurs 'where written', after
the 'rewriting rule' is applied.

        For templates, that is not enough. Method bodies
have to be bound at the point of USE.
 
> Smart pointers are only so smart. They automate some of the things you'd
> have to do by hand with raw pointers, but it's not reasonable to expect
> them to magically be capable of things raw pointers can't do.

        The problem isn't smart pointers per se, the problem
is template instantiation.
 
> > But the example is a special case: just consider:
> >
> > template<class T1, class T2>
> > struct X {
> > smart_ptr<X<T2,T1> > ptr;
> > };
> >
> > to see that it isn't always possible to forward declare things.
>
> What's the problem there?

        The problem is that there is NO ordering
of template instances for which lookup in 'expanded'
classes can work:

        struct X<A,B> { smart_ptr<X<B,A> > ptr; };

        Woops! We cannot lookup X<B,A>, it isn't
instantiated till after. With GCC, if smart_ptr
contains

        p->incr()

g++ tries to lookup 'incr' .. and it can't find
struct X<B,A> to lookup into. What should happen
is that both 'struct X<A,B>' and 'struct X<B,A>'
are layed down first, and THEN, if necessary,
method bodies layed out. g++ incorrectly
expands inline method bodies too early.

-- 
John (Max) Skaller, mailto:skaller_at_[hidden]
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper http://Vyper.sourceforge.net
download Interscript http://Interscript.sourceforge.net

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