Solved

g++ and template compiling

Posted on 1998-12-17
12
427 Views
Last Modified: 2010-04-02
I am trying to make a fully documented data structure library that is Rogue Wave compatible for g++.  However, I am running into a problem of with templates.  I want to separate the .h and .C files rather than putting the implementation in the .h file (which would slow down compliation considerably).  Could someone please direct me to or send me an example where implementation for a data structure (or relevant class) is not in the header file using the g++ compiler?  
0
Comment
Question by:kane7279
  • 6
  • 6
12 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1180437
A template is instanciated at compile time, thus the compiler must "see" the complete definition of the template, or at least the parts that is us using.  Thus it does make much sense to place the implementation of a template in a .cpp file (unless that is the only file that will use the template.
0
 

Author Comment

by:kane7279
ID: 1180438
In C++ it is common to separate the implementation (.C or .cpp) file from the header file (.h).  This makes it easier for a person who wants to use the class since the header file shows what is implemented not how it is implemented.  What I am having trouble with is how a class like this is implemented.  I know this is sometimes done with a pragma statement, but I am not familiar with how to do this.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1180439
>>In C++ it is common to separate the implementation (.C or .cpp) file from the header file (.h).
With a template?

>>I know this is sometimes done with a pragma statement,
That seems unlikely, that is not the type of things that pragmas were designed to do.

If you really wanted to , you could just have the .h file that defines the class also include the .cpp file that contains the implimentation.  That would give you the design you want, throu a round-about means.
0
 

Author Comment

by:kane7279
ID: 1180440
>> If you really wanted to , you could just have the .h file that >> defines the class also include the .cpp file that contains the >> implimentation.  That would give you the design you want, >> throu a round-about means.

Perhaps I wasn't making my question clear.  The above quote tells exactly what I am trying to do.  However, g++ does not handle template instantiation as cleanly as say, Sun's C++ compiler.  As a result, there are two alternatives that are normally chosen.  The first is to put the header and the implementation into one file which, as I said before, slows down compilation time.  The alternative, as I understand it, is to use a pragma declaration. I am looking for an explanation or example of the latter.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1180441
You're being very clear.  I guess I'm not.  The problem is that what you want is not allowed.

Any source code file that uses the template, must "see" the implimentation as well as the definition of the template.  Thus if you place the implimentation in a seperate file, that file will have to be included as well.

What you want to do is a good idea for non-template classes.  It is not permited for template classes.  You weren't able to do this on the Sun c++ compiler were you?
0
 

Author Comment

by:kane7279
ID: 1180442
You can do this with with Sun's C++ compiler.  If you want further information about this you can check out:

http://www.cs.rit.edu/doc/WorkShop/c-plusplus/c%2B%2B_ug/
Templates.new.doc.html#264

The section entitled "Definitions-Separate" gives a very good example.  However, when I try something similar to this in g++, I get a parsing error.

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 22

Expert Comment

by:nietod
ID: 1180443
It does seem that the sun compiler supports this.  But I'm 99% sure that is a non-standard language extension and you are not likely to find it elsewhere.  According to Bjarne Stroustrup's C++ programming language 3rd ed, pages 865-866 the defintion of a template is required at the point of instantiation.  (If you don't have the book, I would recommend you get it.)  Note however that edition was produced before the standard was finalized, but I doubt that this feature has been allowed.
0
 

Author Comment

by:kane7279
ID: 1180444
You can do this with with Sun's C++ compiler.  If you want further information about this you can check out:

http://www.cs.rit.edu/doc/WorkShop/c-plusplus/c%2B%2B_ug/
Templates.new.doc.html#264

The section entitled "Definitions-Separate" gives a very good example.  However, when I try something similar to this in g++, I get a parsing error.

0
 

Author Comment

by:kane7279
ID: 1180445
Sorry about the redundant comment, and thanks for the information.
0
 

Author Comment

by:kane7279
ID: 1180446
Nietod, you were right I just found this:

"Note that the mechanism differs from that of cfront in that template definitions still must be visible at the point
where they are to be expanded. No assumption is made that `foo.C' contains template definitions corresponding to template declarations in `foo.h'."

at http://www.cygnus.com/misc/g++FAQ.html
Thanks again.  You may send a copy of your last comment as an answer so you can receive your earned points.

at
0
 
LVL 22

Expert Comment

by:nietod
ID: 1180447
it does seem that the sun compiler supports this.  But I'm 99% sure that is a non-standard language extension and you are not likely to find it elsewhere.  According to Bjarne Stroustrup's C++ programming language 3rd ed, pages 865-866 the defintion of a template is required at the point of instantiation.  (If you don't have the book, I would recommend you get it.)  Note however that edition was produced before the standard was finalized, but I doubt that this feature has been allowed.

Interestingly, that sounds like cfront supports what you wanted.  That is surprising because I would think that, as a prototype C++ compiler, cfront would not have features like that.  However the g++ compiler is considered to be the closest to the standard.  So if it knowingly doesn't support it, it probably isn't allowed.


0
 
LVL 22

Accepted Solution

by:
nietod earned 120 total points
ID: 1180448
Opps, I'm in comment mode.

it does seem that the sun compiler supports this.  But I'm 99% sure that is a non-standard language extension and you are not likely to find it elsewhere.  According to Bjarne Stroustrup's C++ programming language 3rd ed, pages 865-866 the defintion of a template is required at the point of instantiation.  (If you don't have the book, I would recommend you get it.)  Note however that edition was produced before the standard was finalized, but I doubt that this feature has been allowed.

Interestingly, that sounds like cfront supports what you wanted.  That is surprising because I would think that, as a prototype C++ compiler, cfront would not have features like that.  However the g++ compiler is considered to be the closest to the standard.  So if it knowingly doesn't support it, it probably isn't allowed.


0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now