g++ and template compiling

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?  
kane7279Asked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
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
 
nietodCommented:
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
 
kane7279Author Commented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
nietodCommented:
>>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
 
kane7279Author Commented:
>> 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
 
nietodCommented:
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
 
kane7279Author Commented:
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
 
nietodCommented:
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
 
kane7279Author Commented:
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
 
kane7279Author Commented:
Sorry about the redundant comment, and thanks for the information.
0
 
kane7279Author Commented:
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
 
nietodCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.