[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

g++ and template compiling

Posted on 1998-12-17
12
Medium Priority
?
438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
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 360 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

650 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