-1

In the below snippet, the second example call to max(2,3) doesn't call the function. And there is no compile time/run time error. Can somebody explain what's going on here?

template<class T1>
void max(int a,int b)
{
        cout<<"max() called\n";
}

max<int>(2,3); //this calls max(2,3)
max(2,3); //surprisingly no error and max() isn't called here
anurag86
  • 1,635
  • 1
  • 16
  • 31

1 Answers1

1

You are not using the template. Using it will look something like this:

template<class T1>
void max(T1 a,T1 b)

With everything else the same. Also there is another function that is shadowing it in the std namespace, changing the name of the function to max1 will correct this or removing the namespace. I hope that this helps.

It will look something like this:

#include<iostream>
// because the std namespace is not included there will be no shadowing
template<class T1>
void max(T1 a, T1 b)
{
        std::cout<<"max() called\n";
}

bhristov
  • 3,137
  • 2
  • 10
  • 26