Move template functions to a .cpp file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Move template functions to a .cpp file

 
0
  #1
Mar 22nd, 2009
According to this:
http://www.parashift.com/c++-faq-lit...html#faq-35.13

One way to keep only the function declaration in the .h file is to do this
  1. ////////// file: Tools.h
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T> T Sum(vector<T> &V);


  1. ///////// file: Tools.cpp
  2. #include "Tools.h"
  3.  
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. template <typename T>
  10. T Sum(vector<T> &V)
  11. {
  12. T sum = static_cast<T>(0.0);
  13. for(unsigned int i = 0; i < V.size(); i++)
  14. sum += V[i];
  15.  
  16. return sum;
  17.  
  18. }
  19.  
  20. //this is the key line to tell the compiler to actually make this function for unsigned int
  21. template unsigned int Sum<unsigned int>(vector<unsigned int> &V);

However, what if you want this function for a type that Tools does not know about? ie
  1. template Point Sum<Point>(vector<Point> &V);
will not work because Point has not been defined. If you make Tools depend on Point, but Point already depends on Tools, then there is a circular problem.

Any suggestions?

Thanks,
Dave
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Move template functions to a .cpp file

 
0
  #2
Mar 22nd, 2009
I'm not sure I understand your question. Why can't you just #include "Point.h" ? I do not see anything circular here.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Move template functions to a .cpp file

 
0
  #3
Mar 22nd, 2009
  1. ////// file: Point.h
  2. #include "Tools.h"
  3. class Point
  4. {
  5. public:
  6. double x,y,z;
  7. void OutputSum()
  8. {
  9. // do something using a function from Tools here
  10. }
  11. };

Then if I include Point in Tools is that not a problem?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Move template functions to a .cpp file

 
0
  #4
Mar 22nd, 2009
> Then if I include Point in Tools is that not a problem?

I don't think it's a problem. Have you tried it? If so, and if it doesn't work, then post the entire code.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Move template functions to a .cpp file

 
0
  #5
Mar 22nd, 2009
It does work - but I didn't really want to do that anyway lol, so let me rephrase - is there any way to do this that doesn't involve having to add those two lines (the declaration with specific type and the include) to the .cpp? I've seen people use .txx files - is this what those are for?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Move template functions to a .cpp file

 
0
  #6
Mar 22nd, 2009
I don't know anything about .txx files.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Move template functions to a .cpp file

 
0
  #7
Mar 23rd, 2009
.TXX: DataPerfect Text Storage (A file format to store ASCII-only characters)

Are your sure it was '.TXX' and not '.CXX'?
'.CXX' is a valid C++ extension ...
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Move template functions to a .cpp file

 
0
  #8
Mar 23rd, 2009
yup, its txx.
see section C.5 in this document:
http://public.kitware.com/vxl/doc/de...15.html#SEC158
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Move template functions to a .cpp file

 
0
  #9
Mar 23rd, 2009
Originally Posted by daviddoria View Post
yup, its txx.
see section C.5 in this document:
http://public.kitware.com/vxl/doc/de...15.html#SEC158
I've seen .txx, or variants thereof, it is for compiled template files. Usually the contents of these are various things that need to be compiled to use a given template library, such as type-specific implementations of parts of the library, etc.

Thanx...
Sean
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Move template functions to a .cpp file

 
0
  #10
Mar 24th, 2009
Just build it like you would build a class library ...
Note: Creating a C++ library depends from compiler to compiler ...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC