Let's start with this demo
#include <iostream>
using namespace std;
template <class T>
void abs(T number)
{
if (number < 0)
number = -number;
cout << "The absolute value of the number is " << number << endl;
return;
}
int main()
{
int num1 = 1;
int num2 = 2;
double num3 = -2.1333;
float num4 = -4.23f;
abs(num1);
abs(num2);
abs(num3);
abs(num4);
return 0;
}
The output only showed num3 and num4 in its absolute value form. num1 and 2 were never displayed.
But doesn't program reads from top to bottom? I thought that if num1 is greater than 0, then it should, by all means, go to cout statement, and i.e., print 1.
It seems like this template function is omitting for those that are not negative in the beginning....
I tested without using the template function, and worked fine.
Thank you