473,401 Members | 2,125 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,401 software developers and data experts.

extern usage for template instantiations

sks
Hello all

Is the usage of extern keyword valid for telling the compiler to NOT
instantiate a template and to link it from an another binary?

For example:
Suppose module A's binary contains a template class called "myTemplate", for
which there is an instantiation for 'int' type.

Now suppose a class in module B binary wants to use this template
instantiation, rather than "instantiate" its own, can I use "extern
template<int> class myTemplate;" syntax to use module A's instantiation?

Is this a valid usage ? Is it portable ?

Thanks.
Dec 3 '05 #1
3 2709
sks wrote:
Hello all

Is the usage of extern keyword valid for telling the compiler to NOT
instantiate a template and to link it from an another binary?
No.
For example:
Suppose module A's binary contains a template class called "myTemplate", for
which there is an instantiation for 'int' type.

Now suppose a class in module B binary wants to use this template
instantiation, rather than "instantiate" its own, can I use "extern
template<int> class myTemplate;" syntax to use module A's instantiation?


There is no such syntax.

All you have to do is link your program together. B's use of the
template will generate unresolved references to various Template<int>
member functions. When you link A and B together, those will be
resolved by the instantiation contained in A.

You can prevent the compiler from instantiating Template<int> in module
B by not including the member function definitions in B's translation
unit.

There are two ways to deal with templates. Use a very smart C++
compiler that manages them for you. It maintains a template repository
module where it gathers up and instantiates all of the templates from
the entire program.

Then there is the way it's really done. You use a dumb C++ compiler
which doesn't know how to manage templates for your project (but which
generates templates automatically if it has access to their bodies).

You separate the declaration of the template class from its definition,
just like any other module.

Then in modules where you want the template instantiated, you #include
not just the definition, but the declaration, and write the
instantiation requests in those modules.

Elsewhere, in modules where you just want to use the templates but not
instantiate them, you #include the template class declarations only.
The compiler has no access to the definitions and so it doesn't try to
generate anything.

When I do this, I have some naming convention for the files. I make
both the definition and declaration .h files. One is something like
#include "template.h" and the other #include "template_body.h".

If you want to minimize recompilation dependencies, then what you can
do is make a separate module for each combination of template
parameters. Suppose we have a one-argument template called Template
that is to be instantiated over types A, B, C and D. You can make four
modules, one for each of these combinations. Template_A.cc will
#include the "Template_body.h" header and the "A.h" header that defines
class A, and then just contain the one instantiation request to make
Template<A>. Template_B.cc does the same thing for B and so on.

So now if you touch "Template.h" or "Template_body.h", all of these
template repository units have to be recompiled. But if you change
just A.h, then just Template_A.cc has to be recompiled: the other
templates don't have to be regenerated.

The worst-case scenario would be to have just one module which includes
all your templates, and all classes that those templates are
instantiated over, and puts all the instantiations together. It's easy
to add new material since everything is one place. But that thing has
to be compiled each time you touch any one of the class declarations.
Ouch!

Dec 3 '05 #2
sks wrote:
Hello all

Is the usage of extern keyword valid for telling the compiler to NOT
instantiate a template and to link it from an another binary?


Perhaps you're thinking of the "export" keyword ? Only once compiler I
know of implements export.
Dec 3 '05 #3
sks

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:kb******************************@speakeasy.ne t...
sks wrote:
Hello all

Is the usage of extern keyword valid for telling the compiler to NOT
instantiate a template and to link it from an another binary?
Perhaps you're thinking of the "export" keyword ? Only once compiler I
know of implements export.


What about the content of this link? Is it only true for DLLs? So, if I understand correctly, I could create a DLL that exported basic
32bit instantiations (eg. vector< void* >) of the STL containers, and then
all my client modes could include code like:
extern template class foo< int > ; any 32bit type
and the linker would link the client modules to the appropriate code in
the DLL.


Well, yes, if you aren't looking for anything special by calling out 32
bit types.

http://groups.google.com/group/comp....dad71f2f8239bc


Dec 3 '05 #4

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

Similar topics

2
by: Agent Mulder | last post by:
Hi group, I try to get a reference to a template. In the template, a virtual function is declared. Later in the program, I inherit from classes created by the template. So the template is the...
17
by: Jacek Dziedzic | last post by:
Hello! I have a templated class that serves as a simple vector of elements. template <typename T> class simple_vector : public math_object { // ... lots of simple_vector operations // the...
5
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl...
8
by: | last post by:
This code get's a link error (I'm guessing because the template isn't being instantiated)... // For using templates to simplify deleting items template <class T> // Returns the next ADHL in the...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
by: Boni | last post by:
Dear all, ist it possible to have a template class or func as friend of other class: Folowing returns syntax errors template< typename T> class test{}; template< typename T> int func (Ta); ...
5
by: aryan | last post by:
Is it not allowed to have template function definition in a .cc file? Here is the scenario. The tempage function declaration is in a header file and the definition in a .cc file. The function is...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.