Forward declaration & incomplete template type

Discussion in 'C++' started by verec, Jun 25, 2005.

  1. verec

    verec Guest

    Consider a first version:

    --- drawable.hpp ---
    #include "gcdata.hpp"
    struct drawable {
    ...
    virtual int internal_new_GC(gcdata * gcd) = 0 ;
    } ;

    --- gcdata.hpp ---
    #include "device.hpp"
    struc gcdata {
    ...
    device * d ;
    } ;

    --- device.hpp ---
    #include "drawable.hpp"
    struct device : public drawable {
    ...
    }

    Obviously, this doesn't compile, because the "include stack",
    at the point where ``struct device : public drawable'' is
    reached, looks like
    ...drawable.hpp
    .... gcdata.hpp [drawable.hpp line 2]
    ..... device.hpp [gcdata.hpp] line 2]
    which means that. at the point where the ``struct device'' is
    reached [device.hpp line 3], the struct drawable has not been
    defined yet.

    The fix is to rewrite gcdata with a forward declaration:
    --- gcdata.hpp ---
    // do NOT #include "device.hpp"
    struct device ;
    struc gcdata {
    ...
    device * d ;
    } ;

    So far, so good.

    Now, I'm not using direct pointers, but "smart" pointers

    --- drawable.hpp ---
    #include "gcdata.hpp"
    struct drawable {
    ...
    virtual int internal_new_GC(GCData gcd) = 0 ;
    } ;
    typedef envelope<drawable> Drawable ;

    --- gcdata.hpp ---
    #include "device.hpp"
    struc gcdata {
    ...
    Device d ;
    } ;
    typedef envelope<gcdata> GCData ;

    --- device.hpp ---
    #include "drawable.hpp"
    struct device : public drawable {
    ...
    }
    typedef envelope<device> Device ;

    And now I'm stuck, because the envelope template (which
    is extremely close to boost::shared_ptr/bost_instrusive_ptr)
    has this requirement that T must be a complete type, not a forward
    declaration.

    Hence, the "fix"

    --- gcdata.hpp ---
    // do NOT #include "device.hpp"
    struct device ;
    typedef envelope<device> Device ;
    struc gcdata {
    ...
    Device d ;
    } ;

    does NOT compile ...

    Apart from tinkering with envelope<T> so that it doesn't
    require a complete type (which may or may not be possible),
    anyone sees a way out of this conundrum?

    [BTW: this is a port of Java code: I can change the design
    in only very minor ways, so the requirement that device
    inherits from drawable that uses gcdata that needs device
    has to stay]

    Many Thanks
    --
    JFB
     
    verec, Jun 25, 2005
    #1
    1. Advertisements

  2. verec wrote:
    > [... forward-declaration doesn't work when instantiating templates...]
    > Apart from tinkering with envelope<T> so that it doesn't
    > require a complete type (which may or may not be possible),
    > anyone sees a way out of this conundrum?


    Nope. You're truly stuck. Let's put your code in one module:
    ---------------------------------------------------------------
    template<class A> class TA {}; // your 'envelope'

    struct Data; // forward-declare it...

    struct Base { // OK, let's try to define it
    void foo(TA<Data>); // OOPS! not allowed
    };

    struct Derived : Base { }; // OK, need it here

    class Data {
    TA<Base> tb; // OK, Base and Derived are defined
    };
    ---------------------------------------------------------------
    Oh, wait, maybe we could rearrange it a bit...
    ---------------------------------------------------------------
    template<class A> class TA {}; // your 'envelope'

    struct Base; // forward-declare it...

    class Data {
    TA<Base> tb; // OOPS! Can't do that
    };

    struct Base { // OK, let's try to define it
    void foo(TA<Data>); // OK, Data is defined
    };

    struct Derived : Base { }; // OK
    ---------------------------------------------------------------

    No matter how you move things around, you're faced with something
    that would require an instantiation of a template from a type that
    hasn't been defined yet.

    You will need to go back to a pointer or a reference in one of
    those classes. That means redesign.

    Are you sure that you need to pass an "envelope" to the function
    of your 'drawable' class?

    > [..]


    V
     
    Victor Bazarov, Jun 25, 2005
    #2
    1. Advertisements

  3. verec

    verec Guest

    On 2005-06-25 14:32:24 +0100, "Victor Bazarov" <> said:

    > verec wrote:
    >> [... forward-declaration doesn't work when instantiating templates...]
    >> Apart from tinkering with envelope<T> so that it doesn't
    >> require a complete type (which may or may not be possible),
    >> anyone sees a way out of this conundrum?

    [...]
    > No matter how you move things around, you're faced with something
    > that would require an instantiation of a template from a type that
    > hasn't been defined yet.


    First, thank you Victor, for taking the time to understand the issue.

    > Are you sure that you need to pass an "envelope" to the function
    > of your 'drawable' class?


    Well ... yes. The choices I seem to be left with are:

    1. redesign the code so as to eliminate circular dependencies.
    2. use raw pointers in at least parts of the code
    3. rework envelope

    1. is next to impossible. I'm porting, not redesigning a whole
    system from scratch. That would completely change the nature
    of the project. Given that the port itself, without redesign,
    is already a huge task, choosing this path would mean the
    project death. I simly do not have the 6 years ahead of me
    to complete it :(

    2. If #3 fails, I will very reluctantly pursue this path.

    3. Seems the least bad compromise. I tried:

    --- gcdata.hpp ---
    template <typename T> struct Test {
    T * body ;
    Test(T * q = 0) : body(q) {}
    } ;

    struct device ;
    // typedef envelope<device> Device ;
    typedef Test<device> TDevice ;
    struct gcdata {
    // Device dev ;
    TDevice tdev ;
    } ;

    typedef envelope<gcdata> GCData ;

    And this does compile, probably because the Test<T> template only
    deals with T * and never with T's. But I fear that the conversion
    of envelope<T> might not be as simple ...

    Many thanks for your time
    --
    JFB
     
    verec, Jun 25, 2005
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jobseeker
    Replies:
    2
    Views:
    1,253
    DarkSpy
    Oct 15, 2003
  2. Nobody
    Replies:
    2
    Views:
    50,968
    Nobody
    Feb 12, 2004
  3. qazmlp
    Replies:
    1
    Views:
    857
    Jonathan Turkanis
    Feb 15, 2004
  4. Replies:
    4
    Views:
    729
  5. steprobe
    Replies:
    3
    Views:
    634
    Vladimir Jovic
    Apr 7, 2010
  6. Luca Forlizzi
    Replies:
    4
    Views:
    623
    Luca Forlizzi
    Nov 14, 2010
  7. Jure Erznožnik

    Forward template declaration problem

    Jure Erznožnik, Mar 12, 2011, in forum: C++
    Replies:
    4
    Views:
    1,103
    Jure Erznožnik
    Mar 12, 2011
  8. m0shbear
    Replies:
    1
    Views:
    741
    m0shbear
    Mar 19, 2011
Loading...