i hope when i give, say, 10, 10.0, the program would choose #1, and when i give "hey", it will choose #2, but the result is that, it still choose #1, only when i give variable whose type is char* will choose #2.
i was told "hey" and char* x; both char* type and cause they are actually the addr. i'm puzzled...
and how i code is the right way?
The downside to preferring overloading over specializing is that you can still call the template version if you explicitly specify the template args (which may sometimes be necessary to avoid ambiguities)
1 2 3 4 5 6 7 8 9 10
template <typename T> void f(T); // (a)
void f(int); // (b)
template <typename T> // for this example, assume T=int
void problematic_function()
{
T foo;
f<T>(foo); // calls (a), not (b)
}
Obviously this is a trivial example because <T> doesn't need to be explicitly specified here... but sometimes it needs to be.
It is a poor design idea to demand that users must do two different things in tandem to achieve something that can be achieved by doing just one.
The canonical solution to this problem (and also the problem that function templates can't be partially specialised) is given in the link I had posted earlier. With apologies to those who had read it: