Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
void print(int) { std::cout << "int"; }
void print(long) { std::cout << "long"; }
void print(double) { std::cout << "double"; }
int main() {
print(2.5f);
}double
- Expecting
int:float→intis a conversion,float→doublea promotion, and a promotion beats a conversion. - Expecting an ambiguity error: a call is ambiguous only when the best candidates tie, and here one is a promotion.
- What would
print(2.5f)call if onlyprint(int)andprint(long)were declared? - Why does
print('a')callprint(int)here rather thanprint(long)?