LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2007, 12:48 PM   #1
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
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
 
Old 03-20-2007, 02:33 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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.
 
Old 03-22-2007, 07:35 AM   #3
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
what do you mean by :

"A. inline the template method in the class declaration"
 
Old 03-22-2007, 10:02 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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).
 
Old 03-22-2007, 08:18 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
This is probably the link xhi menentioned.
 
Old 03-23-2007, 08:42 AM   #6
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
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
 
Old 03-23-2007, 09:23 AM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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)

Last edited by xhi; 03-23-2007 at 09:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to clear a std::string buff.clear()? lucky6969b Programming 3 03-17-2006 07:50 AM
how do i replace the clear "clear screen" with the cls command thefedexguy SUSE / openSUSE 2 12-02-2005 05:02 PM
Need help with templates ........ max_rsr Programming 4 04-22-2005 12:53 PM
C++ and templates The_Nerd Programming 4 06-23-2004 01:15 PM
Templates MGLindsey Linux - Networking 0 07-02-2002 11:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration