I have two template functions with the same name ( foo). Their signatures differ only in the type of the second parameter, which depends on the template parameter T. I was surprised that I can use this for overloading depending on whether T::Aor T::Bexisting type exists. Is this what is specifically provided for in the standard (if so, the link will be very appreciated), or am I just not firmly recognizing this as the main permission for overloading?
#include <iostream>
using namespace std;
template<typename T>
void foo(T t, typename T::A* a = 0) {
cout << "Had an A" << endl;
}
template<typename T>
void foo(T t, typename T::B* b = 0) {
cout << "Had a B" << endl;
}
struct HasAnAType {
typedef int A;
};
struct HasABType {
typedef int B;
};
struct HasAAndBTypes {
typedef int A;
typedef int B;
};
int main() {
HasAnAType a;
HasABType b;
HasAAndBTypes ab;
foo(a);
foo(b);
foo(ab);
}
For the background, I discovered that this is possible when studying an implementation std::enable_shared_from_thisthat relies on this type of overload.