What is the difference between A, B and C calls?
#include <iostream>
using namespace std;
template<class T> T max(T a, T b) {
return (a >= b) ? a : b;
}
int main() {
float a = 4.0;
float b = 6.0f;
cout << max(a, b) << endl; //A
cout << max<double>(a, b) << endl; //B
cout << max<double>(4.0, 6.0f) << endl; //C
}
First invocation of max
is actually max<float>
, second and third is the same max<double>
But in second invocation both a
and b
get promoted to double, where in third the example only 6.0f
is promoted to double.
A calls std::max<float>
, since a and b are float arguments. In B and C, you force it to call std::max<double>
, thus both arguments are converted to double.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.