Can template code in cpp file?

Started by
2 comments, last by Asuralm 16 years, 1 month ago
Hi all: I have a very odd problem: In one header file I have

namespace ML_Mesh_NS {

/// Return the mean value of the vertex list
template <class T>
ML_Point GetCentre(const vector<T>& vertex_list);
}


And in a cpp file, there is the implementation of the function GetCentre.

using namespace ML_Mesh_NS;
template <class T>
ML_Point ML_Mesh_NS::GetCentre(const vector<T>& vertex_list){
...
}


When I call the function GetCentre from the main function, it works. And there is another header file:

namespace ML_Mesh_NS{
template <class VT, class FT>
void read_mesh(const char* filename, vector<VT>& vl, vector<FT>& fl);
}


Again, the implementation is in another cpp file. But when I try to test the function, the compiler complains that "undefined reference to the function read_mesh".

using namespace ML_Mesh_NS;
template<class VT, class FT>
void ML_Mesh_NS::read_mesh(const char* filename, vector<VT>& vertex_list, vector<FT>& face_list){
...
}


Why the first one works but the second one doesn't work? I heard that the template code should always be in the header file, but why GetCentre works even the implementation is separated? If I put the code in the header file, will the code be compiled in compile time? As I am worried the speed may be slow if it doesn't compile the code in the header file. Thanks
Asura
Advertisement
Quote:Original post by Asuralm
Why the first one works but the second one doesn't work? I heard that the template code should always be in the header file, but why GetCentre works even the implementation is separated?


It's possible that the version used in the main file was instantiated through way or another from a place where the definition was available.

Quote:If I put the code in the header file, will the code be compiled in compile time? As I am worried the speed may be slow if it doesn't compile the code in the header file.


Rule number one of optimization: never worry about things being slow before you've made them work—optimizing code which doesn't work is silly and a waste of time, because you might throw away all your nice optimizations when you actually try to get the code to work.

As for header files—all C++ code is compiled, including headers.
Quote:Original post by Asuralm
Why the first one works but the second one doesn't work?


Like ToohrVyk said.

Quote:I heard that the template code should always be in the header file, but why GetCentre works even the implementation is separated?


It doesn't have to be in a header. It just has to be visible in the translation unit. One common strategy is to make up some extension like ".inl" (for 'inl'ine code), and #include it from the header; that way, people reading the header file don't see the actual template code, but they still get it, via the preprocessor. Another common strategy is to make explicit instantiations of the template for the values of template parameters that will actually be needed. That works when you need basically the same code for just a handful of known types; the template instantiations behave basically just as if you had written out the code longhand for each type.

Quote:If I put the code in the header file, will the code be compiled in compile time?


It's always "compile time" when something is compiling, hmm? :) C++ does not have any concept of 'interpreting' code, so yes, all the code you give to the compiler is compiled (assuming there are no errors).



As an aside, why on earth would you tag namespace names with a trailing '_NS'? To distinguish them from all the other sorts of names that you put in front of a :: token? Oh wait, there aren't any, are there? :s
thanks Zahlman. That's really helpful. I will look up the details of .inl files.
But another question. Do .inl and .DEF files work with auto tools???

Another question is that is only the inline implementation allowed in the header file?

Is this legal?

class Aclass{void somefunction(){...}}


And what's the difference between inline somefunction() and non-inline somefunction please?



[Edited by - Asuralm on March 4, 2008 4:10:53 PM]
Asura

This topic is closed to new replies.

Advertisement