473,387 Members | 1,606 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,387 software developers and data experts.

Explicit instantiation of templates: any experience?

Has anybody here used explicit instantiation of templates? Has it worked
well? Are there any issues to be aware of?
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 4 '06 #1
3 3982

Steven T. Hatton wrote:
Has anybody here used explicit instantiation of templates? Has it worked
well? Are there any issues to be aware of?
Sure it's worked. Don't do it in headers...don't know what else to
tell yah.

Dec 4 '06 #2
Steven T. Hatton wrote:
Has anybody here used explicit instantiation of templates? Has it
worked well? Are there any issues to be aware of?
The only "problem" or "issue" that I know of is that you have to
predict what template arguments the users of your templates are
going to use. What I've often seen are instantiations of some
calculation templates based on 'float', 'double', 'long double',
and, if applicable, 'complex' variations of those, which makes
it possible to hide the implementation in a library (binary) and
still have only one codebase [for you to maintain].

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '06 #3
Has anybody here used explicit instantiation of templates? Has it worked
well? Are there any issues to be aware of?
There are issues, but they can be managed. Explicit instantiation is
perhaps not one of the more common-knowledge parts of templates in C++,
so consider whether others who may be working on your code will follow
what you are doing. But this should be a minor issue, since the syntax
at least is straightforward. To develop a good understanding of this
topic, I strongly recommend you get hold of "C++ Templates" by
Vandevoorde and Josuttis, then read section 10.5 on Explicit
Instantiation and also Appendix A on the One Definition Rule.

Explicit instantiation requires you to have a very good understanding
of how your templates will be used. For example, if you explicitly
instantiate a non-specialized class, you are enforcing that the class
cannot be specialised in any other code (that would be an ill-formed
C++ program). You have to make it very clear to clients of your class
that they cannot create an explicit specialization for the particular
type you explicitly instantiated. This can get ugly pretty fast if
there are more than a couple of types involved.

A case where explicit instantiation is particularly useful, however, is
when you have a base class with virtual functions and you make a
template subclass for it. Interestingly, if you instantiate an object
of the subclass, the C++ standard does not require the compiler to
instantiate all the virtual functions from the subclass. Therefore, if
you pass the object around by its base pointer, any client code calling
the virtual functions through that base pointer will not trigger an
implicit instantiation of the subclass' virtual function. In practice,
I believe compilers will generally instantiate all virtual functions
when an object is instantiated, since not to do so is generally
counter-intuitive for the average programmer, but I am not willing to
make a blanket statement that they all do. Perhaps someone else can
impart some wisdom here? The relevant part of the C++ standard is
14.7.1.9:

"... It is unspecified whether or not an implementation implicitly
instantiates a virtual member function of
a class template if the virtual member function would not otherwise be
instantiated."

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

Dec 5 '06 #4

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

Similar topics

1
by: K.S. | last post by:
If have template lets call it template class MyTemplate<class T> and the files MyTemplate.cc & MyTemplate.hh In /* MyTemplate.cc */ // definitions .... template class MyTemplate<someType>;...
4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
6
by: Vyacheslav Lanovets | last post by:
Hello, All! I know that Explicit Instantiation actually emits code to obj files (so you can even export them from the module as plain functions or classes). But I found that MSVC7.1 compiler...
3
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void...
5
by: tthunder | last post by:
Hi @all, I am looking for a good (compiler-independent) way to generate meaningful error messages, if specific (unintended) templates are instantiated. e.g. ------------
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.