943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1281
  • C++ RSS
Mar 22nd, 2009
0

Move template functions to a .cpp file

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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);


C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 22nd, 2009
0

Re: Move template functions to a .cpp file

I'm not sure I understand your question. Why can't you just #include "Point.h" ? I do not see anything circular here.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 22nd, 2009
0

Re: Move template functions to a .cpp file

C++ Syntax (Toggle Plain Text)
  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?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 22nd, 2009
0

Re: Move template functions to a .cpp file

> 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.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 22nd, 2009
0

Re: Move template functions to a .cpp file

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?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 22nd, 2009
0

Re: Move template functions to a .cpp file

I don't know anything about .txx files.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 23rd, 2009
0

Re: Move template functions to a .cpp file

.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 ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 23rd, 2009
0

Re: Move template functions to a .cpp file

yup, its txx.
see section C.5 in this document:
http://public.kitware.com/vxl/doc/de...15.html#SEC158
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 23rd, 2009
0

Re: Move template functions to a .cpp file

Click to Expand / Collapse  Quote originally posted by daviddoria ...
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
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Mar 24th, 2009
0

Re: Move template functions to a .cpp file

Just build it like you would build a class library ...
Note: Creating a C++ library depends from compiler to compiler ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Re: How to use double pointers in C++?
Next Thread in C++ Forum Timeline: need help with matrix





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC