Templates and header files

K

karlman

Hello!
Here's a question regarding templates:
Suppose I have these three files containing the following:

print.hpp:
template < typename T >
void print( T x );
print.cpp:
template <typename T>
void print( T x )
{
std::cout << x << std::endl;
}
driver_print.cpp:
#include "print.hpp"

int main()
{
print( "Whazz up!" );
print( 134 );

return 0;
}
This doesn't work neither with g++ (3.x.x) nor bcc32 (5.5.1).
But when you change the files to the following it works:

print.hpp:
template < typename T >
void print( T x )
{
std::cout << x << std::endl;
}
driver_print.cpp:
#include "print.hpp"

int main()
{
print( "Whazz up!" );
print( 134 );

return 0;
}
The book I'm reading warns you of this, but I'm interested in why is this
so.
Thank you for your answers,
Karlo.
 
R

Rolf Magnus

karlman said:
Hello!
Here's a question regarding templates:
Suppose I have these three files containing the following:

print.hpp:
template < typename T >
void print( T x );
print.cpp:
template <typename T>
void print( T x )
{
std::cout << x << std::endl;
}
driver_print.cpp:
#include "print.hpp"

int main()
{
print( "Whazz up!" );
print( 134 );

return 0;
}
This doesn't work neither with g++ (3.x.x) nor bcc32 (5.5.1).
But when you change the files to the following it works:

print.hpp:
template < typename T >
void print( T x )
{
std::cout << x << std::endl;
}
driver_print.cpp:
#include "print.hpp"

int main()
{
print( "Whazz up!" );
print( 134 );

return 0;
}
The book I'm reading warns you of this, but I'm interested in why is
this so.

This is because a template isn't a real function. It's just a recipe
that tells the compiler how to generate it. So no actual code is
generated before the template gets instantiated, i.e. before you call
print. But to be able to generate the code, the compiler would need the
full definition of the template. But the compiler sees only one
translation unit at a time, so if the tempalte definition is in another
translation unit, it can't do the template instantiation.
 
T

tom_usenet

This is because a template isn't a real function. It's just a recipe
that tells the compiler how to generate it. So no actual code is
generated before the template gets instantiated, i.e. before you call
print. But to be able to generate the code, the compiler would need the
full definition of the template. But the compiler sees only one
translation unit at a time, so if the tempalte definition is in another
translation unit, it can't do the template instantiation.

[To the OP]
However, there is a standard facility to give the compiler access to
template definitions in other translation units: the "export" keyword.
It isn't widely implemented, and neither GCC nor MSVC++ supports it.
If you want to try it out, you'll have to buy Comeau C++ from
www.comeaucomputing.com (see also
http://www.comeaucomputing.com/4.0/docs/userman/export.html). I think
Intel C++ also has it as a so far undocumented feature that's off by
default. The next Borland C++ may also have it. (Note all these
compilers have something in common - they use the EDG C++ front end -
see http://www.edg.com/cpp.html).

Tom
 

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

Members online

Forum statistics

Threads
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top