Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 02-05-2009
Member
 
Join Date: May 2009
Posts: 3,613
Explicit template instantiation

I was wondering in which case the explicit instantiation of a class or function template must be used? Or otherwise what is the advantage of using explicit instantiation?
Reply With Quote
  #2  
Old 02-05-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Explicit template instantiation

To save compilation time, or to hide an implementation
Reply With Quote
  #3  
Old 02-05-2009
Member
 
Join Date: Feb 2008
Posts: 194
Re: Explicit template instantiation

For example:

Suppose you have code lib calculation matrix. It works with the choice, float, double and long double. Of course not to 3x rewrite the same code, you have made a template.

But now, the problem is that you already do not want to release the source code (or with templates, and export support is required) and then you do not want your template is instantiated with a type other than the 3 planned.

To be restricted to 3 types, there is in playing on static_assert or any other equivalent system. But to hide the code, there is only one way: just deliver the statements of the code, without implementation, and explicitly instantiate your template with the 3 types in a lib, which will be provided.

It allows also to win perfs: the compiler does not generate code to instantiate it every time a template, since the code it is not available. This is the linker that will work with the lib provided only once.

Otherwise, each unit of compilation, all templates are instantiated and generate the code, then the linker at the end is several instantiations and needs with a single linker. It takes a long time.
Reply With Quote
  #4  
Old 02-05-2009
XSI XSI is offline
Member
 
Join Date: May 2008
Posts: 271
Re: Explicit template instantiation

What is the interest of the manufacturer only instantiate a class template explicitly, and not the whole class:

template MyClass<double>::MyClass(); // generate constructor

vs

template class MyClass<long>; // generate the whole class
Reply With Quote
  #5  
Old 02-05-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: Explicit template instantiation

In large projects, where hundreds of source files have to be compiled on every build, the long compilation time can be reduced significantly if templates are explicitly instantiated all at once. This is because when the compiler has to instantiate a template, its thread of execution is interrupted by the need to generate the code for that template specialization (template for a specific type), and only then can it resume parsing. When hundreds of sporadic templates are to be instantiated, it can make a difference. Therefore, it's best to instantiate all templates in one source file explicitly, freeing the compiler from repeated interruption of its processing.

Source: devx
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Explicit template instantiation"
Thread Thread Starter Forum Replies Last Post
Where can I find bank check template for Microsoft Word or Template aMADeO! MS Office Support 2 28-01-2012 03:50 PM
Explicit Locks In Java Henryosa Software Development 5 13-02-2010 05:46 PM
Meaning of Explicit and Implicit Conversion Sheenas Software Development 5 30-01-2010 12:42 PM
What is the difference between Declaration and Instantiation an object? ArunJ Software Development 1 28-02-2009 07:10 PM
Difference between Implicit and Explicit Declaration vinodpathak_214 Software Development 3 16-01-2009 10:00 AM


All times are GMT +5.5. The time now is 08:26 AM.