I have written a code with template but it works only with Visual Studio( not in Dev c++ or any online compiler. I don't understand why.
#include <iostream>
using namespace std;
template <class Q1,class Q2,class Q3> // but when i write instead of 3 classes 1 class it will work
//everywhere, how could it be possible?
void min(Q1 a, Q2 b, Q3 c) {
if (a <= b && a <= c) {
cout << "\nMinimum number is: " << a << endl; }
if (b < a && b < c) {
cout << "\nMinimum number is: " << b << endl; }
if (c < a && c < b) {
cout << "\nMinimum number is: " << c << endl; }
}
int main()
{
double x,y,z;
cout << "Enter 3 numbers: " << endl;
cin >> x;
cin >> y;
cin >> z;
min(x, y, z);
}
The function std::min
is used implicitly. That's because overload resolution favours non-template functions over template ones, and some compiler toolsets allow std::min
to be reachable via the #include
s you have (the only thing the C++ standard has to say on the matter is that std::min
must be available once #include <algorithm>
is reached).
Dropping using namespace std;
is one fix, and is a good idea anyway. Tutorials often use it for clarity, but it's rare to find it in production code.