clear up some questions about templates
Hello all & sundry.
I have some questions about templates. I use templated classes extensively in my work (computer science / evolutionary algorithms). If my system needs to use a float instance if the template I setup a file like so (NodeLib.cpp) #include "Node.cpp" template class Node <float> ; This is fine when the whole class is templated, however I recently wrote a class in which i only required a templated method like so class ID_Node { public: template <class dataType> bool TestSequence(Node <dataType> *); } I could not subsequently write an object code file like NodeLib.cpp which would instansiate the parictular method so I could link it to a float instance of the class Node. Does anyone know how to do this?. Node that I could have simply included the file "ID_Node.cpp" whereever I used the method but this strikes me as an unsustainable practice. Also, while we're on the subject of templates, does anyone know how the stl vector and list templates classes are able to deal with the arbitrary data types that they are given in any c++ code?. Could I use the same mechanism to get around having to instansiate a file like NodeLib.cpp Thanks in advance. Gearoid |
a couple of options
A. inline the template method in the class declaration B. you can manually instantiate the templates that you need in the .cc file .. (maybe others?) example of B myclass.cc Code:
#include "myclass.hh" Code:
class MyClass your question about the stl containers is answered with solution A (afaik). take a look at your c++ distro and you can easily find out for sure how your implementation handles it. |
what do you mean by :
"A. inline the template method in the class declaration" |
myclass.h
Code:
class MyClass before i get jumped, i will admit that i dont know if actual inlining takes place here. my previous use of inlining was referring to defining the function in the class header (inlining here is probably implementation dependent anyways). |
This is probably the link xhi menentioned.
|
this clears up a lot of issues, I extend my warmest thanks. Its a pity that templating is such a bitch. Both the implementation syntax, the error messages and compiler behavior leaves a lot to be desired in a realively mature language such as c++. Has there ever been any rumblings about this in the upper echelons of the language architects?. Why wasn't there a generic type created which would have indicated to the system that this was the templated variable? (thats probably awfully simplistic), rather than having to mangle your code with arcane and esoteric template references
|
with great power comes great responsibility.. (or something like that, maybe it was great headaches?) besides if it was not the bitch that it is, it would be java. eww.
(and yes, tuxdev's link was the faq that i was talking about) |
All times are GMT -5. The time now is 04:42 AM. |