When the compiler does not select the overload you expect, it can lead to compilation errors or unexpected behavior. To debug this, you can follow these steps:
static_assert
 and typeid
 to print out the deduced types.For example, consider this code:
#include <iostream>
void Print(int x) {
std::cout << "Print(int)\n";
}
void Print(double x) {
std::cout << "Print(double)\n";
}
int main() {
Print(10);// calls Print(int)
Print(10.0);// calls Print(double)
// float converted to double
Print(10.0f); // calls Print(double)
// char promoted to int
Print('a');// calls Print(int)
}
Print(int)
Print(double)
Print(double)
Print(int)
By analyzing the types of the arguments and the parameter types of the overloads, you can understand which overload will be called in each case.
Answers to questions are automatically generated and may not have been reviewed.
Learn how the compiler decides which function to call based on the arguments we provide.