473,626 Members | 3,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forward declaration & incomplete template type

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.h pp"
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<drawab le> Drawable ;

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

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

And now I'm stuck, because the envelope template (which
is extremely close to boost::shared_p tr/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


Jul 23 '05 #1
2 2395
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
Jul 23 '05 #2
On 2005-06-25 14:32:24 +0100, "Victor Bazarov" <v.********@com Acast.net> 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

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
37066
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't explicitly say about enum's forward declaration, but one example shows it in 18.2.1, clause 4:
2
14144
by: jobseeker | last post by:
From: jobseeker95479@yahoo.com (jobseeker) Newsgroups: comp.lang.c++.moderated Subject: template and forward declaration NNTP-Posting-Host: 131.233.150.22 I have defined a class that contains a data member of a function type. The signature of the function type take a pointer of the class itself as a parameter: class Softkey; // forward declaration
6
2843
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error: "field `foo' has incomplete type" for the following code: #include <iostream> #include <vector>
11
2800
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
23
3839
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
7
5983
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
11
2589
by: Martin Eisenberg | last post by:
Hi Antoine, just redirecting you... Antoine Trux wrote: > Hi, > > Is the following code legal: > > ------> code starts here <------ > #include <stddef.h>
6
8611
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in myfile.h template<typename T,typename R> class some_class;
8
28450
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.