LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   clear up some questions about templates (https://www.linuxquestions.org/questions/programming-9/clear-up-some-questions-about-templates-539063/)

gearoid_murphy 03-20-2007 12:48 PM

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

xhi 03-20-2007 02:33 PM

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"
...

template void MyClass::templateFunction<int>(int name, int& foo);
template void MyClass::templateFunction<float>(int name, float& foo);
template void MyClass::templateFunction<string>(int name, string& foo);

template<typename t> void MyClass::templateFunction(int name, t& foo)
{
}

myclass.hh
Code:

class MyClass
{
    ....
    template<typename t> void templateFunction(int name, t& foo);
}

i have used both methods, and generally if i can, i stick with A. however i have had cases where i want to use a static method in a template method, so i have used B also. there is a good c++ faq about this out there, that goes into more detail and gives a good explanation of why it is. i dont have the link handy though.

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.

gearoid_murphy 03-22-2007 07:35 AM

what do you mean by :

"A. inline the template method in the class declaration"

xhi 03-22-2007 10:02 AM

myclass.h
Code:

class MyClass
{
    ....
    template<typename t> void templateFunction(int name, t& foo)
    {
        // template is defined in the class definition in the header. clients of this
        // class will be able to generate appropriate versions of this function as you would expect them to.
    }
}

this will allow the compiler to generate the function at the point it is used. otherwise, without the definition here or in option B, you will get linker errors because it will never generate the functions.

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).

tuxdev 03-22-2007 08:18 PM

This is probably the link xhi menentioned.

gearoid_murphy 03-23-2007 08:42 AM

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

xhi 03-23-2007 09:23 AM

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.