Why will i use explicit instantiation of a template, for a type? If I do not use explicit instantiation, the template is used to create the necessary function then what is the use of explicit instantiation? -
template <class Any>
-
void Swap (Any &, Any &);
-
// template prototype
-
-
template <> void Swap<job>(job &, job &);
-
// explicit specialization for job
-
-
int main(void)
-
{
-
-
template void Swap<char>(char &, char &);
-
// explicit instantiation for char
-
-
short a, b;
-
Swap(a,b);
-
// implicit template instantiation for short
-
-
job n, m;
-
Swap(n, m);
-
// use explicit specialization for job
-
-
char g, h;
-
Swap(g, h);
-
// use explicit template instantiation for char
-
-
}
-
In the above eg. explicit instantiation is done for char type. What is the use of this?? If the compiler can make use of the template to make a fn for char type.
<eg. taken from C++ Primer>
If there are any refrences that can help me clear my concept, pls do include those.
4 2166 Banfa 9,065
Expert Mod 8TB
You would use an explicit specialisation if the generalised template function does not suffice for the type in question. By declaring the specialisation - template void Swap<char>(char &, char &);
You are telling the compiler it exists somewhere, that then leaves you free to put the actual specialisation of the function for the type into another source file.
However this is not a good way to do it, as a rule of thumb you should never specialise function templates because you can just overload them (normally), i.e. you declare your function - void Swap(char &, char &);
In function overloading the compiler/linker will always prefer a non-template function that matches exactly to the use of a template specialised or otherwise.
Thanks Banfa. But i wanted to know about Explicit instantiation
template void Swap<char>(char &, char &);
I think you have explained about Explicit specialisation
template<> void Swap<char>(char &, char &);
You use explicit instantiation when the compiler may not be able to figure out the coorect type to use with the template specialization.
There is no term explicit initialization.
So this code:
tells the compiler to use char and not promtote the char to an int or long or short, or some other type.
Maybe you have a class with an operator char and your template has a class T. The compiler will use the class type in the template which is not what you want. By explicitly using char you force the compiler to convert the class type to a char by calling the operator char and then to use that result in the template function call.
Banfa 9,065
Expert Mod 8TB
@suvarna271 you're right, I'm sorry.
A use of an explicit instantiation - template void Swap<char>(char &, char &);
would be when writing a library that you wanted to include the code for template using the type of the explicit instantiation but where the code of the library did not use that type. The explicit instantiation forces the compiler put the code for that template instantiation into the object whether it is used or not.
Off the top of my head I can not think of a good example as to why you would want to do this but that is what it is for.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Arne Petersen |
last post by:
Hy,
I've got a problem with member function templates compiled into
libraries.
I'm trying to get a library collection (coded for GNU gcc, where its
compiled completly) being compiled on Visual...
|
by: CoolPint |
last post by:
In the code below, shouldn't the function call minimum(a,a); result in
compilation error? I read in Lippman's C++ Primer 3rd Edition on page
521, the call should be ambiguous.
But on g++...
|
by: WittyGuy |
last post by:
Hi,
What is the major difference between function overloading and function
templates?
Thanks!
http://www.gotw.ca/resources/clcm.htm for info about ]
|
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...
|
by: George Sakkis |
last post by:
Hi all,
I have the following two template function definitions:
template <typename Container, typename Sepatator>
string join(const Container& c, const Sepatator& s) {
// ...
}
template...
|
by: Alfonso Morra |
last post by:
I have written a class, and several of it's methods are function
templates. When I compiled the code, I realized that I had made some
typos in the code - but the compiler seemed to skip right over...
|
by: recover |
last post by:
#include <stdio.h>
template<class T>
class TpHello
{
public:
int GetHash(){return 0;}
protected:
private:
T a;
|
by: Steven T. Hatton |
last post by:
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...
|
by: William Xu |
last post by:
Compiling:
template <class T = int>
T foo(const T& t)
{}
int main(int argc, char *argv)
{}
gcc complains:
|
by: matthias.neubauer |
last post by:
I have problems understanding how overloading of function templates
works.
Consider first the following code without any function templates ...
int foo(const char *& c)
{
return 0;
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |