I can’t figure out why the following code compiles fine:
#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> // Note the order of A and B.
void foo(B x) {
bar((A)x);
}
int main() {
int x = 1;
double y = 2;
foo<int>(x); // Compiles OK.
foo<double>(y); // Compiles OK.
return 0;
}
But if I switch the order of A and B as below, then it won’t 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> // Order of A and B are switched.
void foo(B x) {
bar((A)x);
}
int main() {
int x = 1;
double y = 2;
foo<int>(x); // error: no matching function for call to ‘foo(int&)’
foo<double>(y); // error: no matching function for call to ‘foo(double&)’
return 0;
}
EDIT: Ad-hoc explanations are welcome, but would be better if someone can point out exact what the spec. says. Thanks!
In the first one, the compiler knows that
Aisintbecause you specifically tell it so withfoo<int>, and it knows thatBis alsointbecause of the parameter that you pass it. So bothAandBare known or can be deduced (you could say:Ais supplied,Bis implied).However, in the second one, since
Bcomes first andAdoesn’t appear in the parameter list, the compiler can’t tell whatAis and gives you an error. You’re explicitly telling it whatBis withfoo<int>, and then the parameter you pass is also aBwhich, at the call, is anintwhich agrees with your previous explicit definition ofB, but no mention is made ofA, implicitly or explicitly, so the compiler must stop and error.You don’t really need the standard for this, it’s just common sense. What on earth would
Abe in the second one?Thanks for asking this question though, because I didn’t realise you could explicitly specify some parameters and implicitly specify others in the parameter list before this.