473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Templates and header files

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.
Jul 22 '05 #1
2 1344
karlman wrote:
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.

Jul 22 '05 #2
On Thu, 10 Jun 2004 11:53:50 +0200, Rolf Magnus <ra******@t-online.de>
wrote:
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/d...n/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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
1
by: Leslaw Bieniasz | last post by:
Cracow, 15.09.2004 Hi, I am writing a big C++ project using BCB 4.0. The project consists of several dlls and exes. Each of the dll is composed of several units (that is cpp files with...
3
by: Ruben Campos | last post by:
I've found a problem with types defined inside a template. With a non-template class, I can write the following: // MyClass.hpp class MyClass { // ... typedef unsigned int MyType; MyType...
3
by: mark wade | last post by:
Thanks for your help in advance. Hello, I am just learning HTML and are using HTML-KIT. I am wondering if HTML-Kit has a bunch of templates to choose from to build a website. I see that...
5
by: Robert Bralic | last post by:
Dear, I wreated a small program that make a linked list of factorials of numbers, I don't have expirience with templates so I will bee thankfull if anybody can make the same with templates(change...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
12
by: Ben | last post by:
I'm kind of new to creating templates. I've made some small class and function templates in the past and I have used quite of bit of the STL, but I am having problems tyring to create templates. ...
14
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
4
by: zfareed | last post by:
#include <iostream> #include <fstream> using namespace std; template<class ItemType> class SortedList { private:
3
by: bav.272304 | last post by:
I have a code which uses external library with templates. The separate compiling of project files gets definition of the same symbols in different object files, so linking fails. Actually,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.