we are going to discuss about the template
Template is a skeleton for which we are going to give different body .
where it can be used
one important thing comes in to my mind ... that is logs
I have written one third party app , also it has one log to write some important logs
well,
the input param could be either int , string ,double or float .. I
guess , for logs we are not using more than this primitive
Code
template <class T>
void log_function(T in , const char* tag)
{
cout<<tag << "\t" << in ;
}
see , how good it is , otherwise we are going to write a overload function which will take the different data type
by the way , when call is made
Ex: log_function(15 ,"TAG LINE");
log_function("hello world" ,"TAG LINE");
log_function(15.45 ,"TAG LINE");
compiler generates function for each data type . So, it internally does the overloading :)
good news is , we can avoid manual mistakes
No comments:
Post a Comment