ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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.
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).
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.