Templates

The idea of the function prt() is to print a number, independent of the data type. We can use a template (or do it in a different way). The template <class T> applies for the general case. The specialization happens in template <>.

#include <iostream>
using namespace std;

template <class T> void prt(T b); 

template <class T> 
void prt( T b)
{
  char buffer[100]; 
  sprintf( buffer, " %d", b); 
  cout << buffer << "\n";
}

template <> 
void prt( float b)
{
  char buffer[100]; 
  sprintf( buffer, " %g", b); 
  cout << buffer << "\n";
}

main()
{
  float x = 5;
  int i = 4; 
  prt( x); 
  prt( i); 
}