Boost logo

Boost :

From: Ross Smith (ross.s_at_[hidden])
Date: 2001-05-29 15:42:24

John Max Skaller wrote:
> The following works in C:
> struct X { struct X* x; };
> struct Y { struct Y* y; };

From analogy with your previous example I assume the structure names
there were meant to be the other way around. (If that's not so, then
I've missed your point somewhere and you'll need to amplify.)

> and replacing an X* with a smart_ptr<X> had better work, or templates
> are utterly useless for building recursive data structures.
> Perhaps you wanted me to prepend:
> class X;
> class Y;

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 ***. It
wuill work if you put the body of ~X() out of line. Your original
example with smart pointers was exactly analogous to this, and I see no
reason to expect it to work.

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.

> 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? It looks perfectly legal to me, and GCC 2.95.3
and MSVC 6.0 both agree with me.

Ross Smith <ross.s_at_[hidden]> The Internet Group, Auckland, New Zealand
        "Hungarian notation is the tactical nuclear weapon of
         source code obfuscation techniques." -- Roedy Green

Boost list run by bdawes at, gregod at, cpdaniel at, john at