Author Topic: Question about c++ function/function templates?  (Read 1199 times)

0 Members and 1 Guest are viewing this topic.

pllaybuoy

  • Guest
Question about c++ function/function templates?
« on: October 09, 2012, 10:09:36 pm »
Whats the difference between a function template , an explicit instantiation , an implicit instantiation and an explicit specialization template ?
 
 following is a function template
 
 template <typename m> //generic function for swapping the values of two arguments
 void swap(m &a,m &b){
 m temp;
 temp=a;
 a=b;
 b=temp;
 }
 
 here is an explicit specialization template for the above function in order to make it swap second element of two structures (if i am not wrong )
 
 struct userinfo(std::string name; int age;};
 
 userinfo user1,user2;
 user1={shahab,16};
 user2={brendon,15};
 
 template <> void swap<userinfo>(userinfo &a ,userinfo &b){
 int temp;
 temp=a.age;
 a.age=b.age;
 b.age=temp;
 }
 
 So the questoin begins here,why would you use an explicit specialization in the first place ? i mean look at that ******* function header , its so hard and messed up .
 secondly what would be implicit and explicit instantiation in this case ?
 I've been trying to digest this for previous 2 days but no progress =/


(not a pro just a determined learner , so please dont go bashing about it if i sounded stupid ,thank you :) )

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Question about c++ function/function templates?
« Reply #1 on: October 11, 2012, 07:38:43 pm »
An explicit specialization, or just 'template specialization', is used to perform an operation specifically for a given data type.  Consider a template that will, in theory, work for every basic integral data type.  This same template may also work with some other data types but in those cases, might require a little special handling.  Have a look at the following example code:
Code: (cpp) [Select]
#include <iostream>
#include <limits>
#include <string>

using namespace std;

static streamsize _n_lim = numeric_limits<streamsize>::max();

export template <typename T> void get_input( T *buffer ) {
   while ( !(cin >> *buffer) ) {
      cout << "Invalid input!" << endl;

      cin.clear();
      cin.ignore(_n_lim, '\n');
   }
}

template <> void get_input<char>( char *buffer ) {
   cin.clear();
   cin.ignore(_n_lim, '\n');

   cin.get(*buffer);

   return;
}

template <> void get_input<string>( string *buffer ) {
   getline(cin, *buffer);

   return;
}


The above code introduces a single template with two template specializations.  It is possible to further extend this template for other data types, if desired, simply by providing additional template specializations explicitly(hence 'explicit specialization').

-Xires

pllaybuoy

  • Guest
Re: Question about c++ function/function templates?
« Reply #2 on: October 11, 2012, 09:49:19 pm »
Hey thank you for the response :)
I got that btw and made a post about it here too , thanks once again bro