473,406 Members | 2,369 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.

Explicit Initialisation of Function Templates

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?


Expand|Select|Wrap|Line Numbers
  1. template <class Any>
  2. void Swap (Any &, Any &);
  3. // template prototype
  4.  
  5. template <> void Swap<job>(job &, job &);
  6. // explicit specialization for job
  7.  
  8. int main(void)
  9. {
  10.  
  11. template void Swap<char>(char &, char &); 
  12. // explicit instantiation for char
  13.  
  14. short a, b;
  15. Swap(a,b);
  16. // implicit template instantiation for short
  17.  
  18. job n, m;
  19. Swap(n, m);
  20. // use explicit specialization for job
  21.  
  22. char g, h;
  23. Swap(g, h);
  24. // use explicit template instantiation for char
  25.  
  26. }
  27.  
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.
Feb 28 '13 #1
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

Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 28 '13 #2
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 &);
Feb 28 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. void Swap<char>(a, b); 
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.
Feb 28 '13 #4
Banfa
9,065 Expert Mod 8TB
@suvarna271 you're right, I'm sorry.

A use of an explicit instantiation
Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 28 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
3
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++...
16
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 ]
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...
1
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...
7
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...
2
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
3
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...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
7
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; }
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.