Zeppe
9/10/2008 8:30:00 AM
krchandu@gmail.com wrote:
> inline int const& max (int const& a, int const& b)
> {
> cout << "Normal function called \n";
> return a<b?b:a;
> }
>
> // maximum of two values of any type
> template <typename T>
> inline T const& max (T const& a, T const& b)
> {
> cout << "Template function with 2 param called \n";
> return a<b?b:a;
> }
>
>
> int main()
> {
> ::max(7, 42); // calls the nontemplate for two ints
> ::max<>(7, 42); // calls max<int> (by argument deduction)
> }
>
>
> Here, function call max<>(7, 42); instantiate the max template for int
> as if it was declared and implemented individually. But there is a non-
> template function with same signature. How can it be possible to have
> two functions with same signatures?
Template specialisation: after the template function declaration, you
declare a specialisation for int:
template<>
inline int const& max(int const& a, int const& b)
{
cout << "specialised function called \n";
return a<b?b:a;
}
In this case, the second function call will use the specialised function.
Best wishes,
Zeppe