+ Reply to Thread
Results 1 to 2 of 2

Thread: What is function template in C++?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    29

    What is function template in C++?

    Hi All,

    What is function template in C++?

    Please provide me simple example.

    Regards.

  2. #2
    Hi,

    Template is used for generic programming.
    It allows user to create an implementation that can work with many different types.

    Following is the example of function template.

    It allows user to use this function for many different type like int, double, std::string as long as the type used supports ">" operator.

    Code:
    template <typename T1>
    T1 Max(T1 obj1, T1 obj2)
    {
    	return (obj1 > obj2) ? obj1 : obj2 ;
    }
    
    void UseFunctionTemplate()
    {
    	int			nMax	= Max<int>(10, 20) ;
    	double		dMax	= Max<double>(10.0, 20.0) ;
    	std::string	strMax	= Max<std::string>("10.0", "20.0") ;
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts