Using templates

S

Sunil Varma

Hi,

I'm trying to write template functions and use them.

Is it possible to declare a template function in a .h file and define
it in a .cpp file.

Ex:

//temp_func.h
template <class T>void swap(T &left,T &right);

//temp_func.cpp
template <class T>void swap(T &left,T &right)
{
T temp = left;
left = right;
right = temp;
}

//main.cpp
#include <iostream>
#include "temp_func.h"

using namespace std;
main()
{
int a = 10, b = 20;
cout<<"Before swapping: a = "<<a<<", b = "<<b<<endl;
swap(a,b);
cout<<"After swapping: a = "<<a<<", b = "<<b<<endl;
}

When I built the above code, I got the following error:

undefined reference to 'void swap<int>(int&,int&)'

I know that the instantiation of the swap() function is not available
in temp_func.o.

But, how can one make the above code build successfully.

The above code builds properly if I define the swap() function in
the .h file itself.

How do the STL algorithms work?

Are those functions defined in the headers itself.

Thanks in advance.

Regards
Sunil
 
J

James Kanze

I'm trying to write template functions and use them.
Is it possible to declare a template function in a .h file and
define it in a .cpp file.

Yes, but...
//temp_func.h
template <class T>void swap(T &left,T &right);

You need to declare it extern here. Which is a problem, since a
lot of compilers aren't very up-to-date, and don't support
extern.
//temp_func.cpp
template <class T>void swap(T &left,T &right)
{
T temp = left;
left = right;
right = temp;
}

This is, obviously, the only reasonable organization. Given
that compilers don't support extern, howver, what you have to do
is add an ``#include "temp_func.cpp"'' to the end of your .h
file (and of course, ensure that the .cpp is exported along with
the .h file, when you provide a library).

A common convention is to use still a different naming
convention for these files. G++, for example, terminates them
with .tcc (as opposed to .cc and .hh). Typically, they are
designed to work only when included at the end of the .hh, and
shouldn't be included anywhere else.

Another possibility would be to use export when available, using
conditional compilation. Basically, your header looks something
like:

#ifndef TEMP_FUNC_CPP_andSomeRandomJunk

export template< typename T > void swap( T& left, T& right ) ;

#ifdef export
#include "temp_func.cpp"
#endif
#endif

If the compiler doesn't support export, you invoke it with
something like -Dexport= or /Dexport=, whatever is necessary to
define "export" as an empty string.

[...]
How do the STL algorithms work?
Are those functions defined in the headers itself.

It depends on the implementation---g++ does as I described, it
puts the implementations in a .tcc file, which is included from
the .hh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

templates?? 2
templates 4
I need help 1
May anyone tell me why the values aren't swapping? 0
using my template class 11
templates help 1
Template function specialization 4
Unrolling loops using templates 6

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,839
Latest member
MartinaBur

Latest Threads

Top