I cannot understand why the following code compiles:
#include <iostream>
void bar(int x) {
std::cout << "int " << x << std::endl;
}
void bar(double x) {
std::cout << "double " << x << std::endl;
}
template <typename A, typename B>
void foo(B x) {
bar((A)x);
}
int main() {
int x = 1;
double y = 2;
foo<int>(x);
foo<double>(y);
return 0;
}
But if I switch the order Aand B, as shown below, it will not compile:
#include <iostream>
void bar(int x) {
std::cout << "int " << x << std::endl;
}
void bar(double x) {
std::cout << "double " << x << std::endl;
}
template <typename B, typename A>
void foo(B x) {
bar((A)x);
}
int main() {
int x = 1;
double y = 2;
foo<int>(x);
foo<double>(y);
return 0;
}
EDIT: Special clarifications are welcome, but it would be better if someone could specify exactly what the spec is. He speaks. Thank!
source
share