0

Void prints out double not int

Why does a void function that prints out two variables sum print a double when it receives a double and an int? #include <iostream> using namespace std; template <class T, class U> void smaller(T a, U b) { cout << a+b; } int main () { int x=72; double y=15.34; smaller(x, y); }

5th Aug 2017, 9:41 PM
Philip Gard
3 Réponses
+ 5
http://icecube.wisc.edu/~dglo/c_class/promo_conv.html describes when types are "promoted" e.g., because one data type is assumed to be more efficient and "converted" e.g., to "raise an expression to the largest datatype" These are guards against undefined behavior and efficiency/precision loss.
5th Aug 2017, 9:58 PM
Kirk Schafer
Kirk Schafer - avatar
+ 4
Can we see the code? Most likely one of the variables was a double. So the int was upcasted.
5th Aug 2017, 9:43 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Thanks for the help😊
5th Aug 2017, 10:09 PM
Philip Gard