I'm trying to use a combination of template specialization and implicit conversion to sort templated types in to 3 categories:
- types that can be implicitly converted to a string
- types that can be implicitly converted to a double
- types that cannot be implicitly converted to a string nor double
In this attempt of mine, however, all types that could have been converted into either a double or a string end up in the default case of type T.
template<>
void Foo(std::string s)
{
//can be converted to a string
}
template<>
void Foo(double d)
{
//can be converted to a double
}
template<typename T>
void Foo(T t)
{
//cannot be converted to a string or a double
}
struct Other {};
int main()
{
Foo("bar");// creates Foo(const char*) instead of converting to string
Foo(1);// creates Foo(int) instead of converting to double
Other o
Foo(o);// creates Foo(Other)
}
On the other hand, implicit conversion works if the templating is removed, but at the cost of being able to handle the "neither" types.
void Foo(std::string s)
{
//can be converted to a string
}
void Foo(double d)
{
//can be converted to a double
}
struct Other {};
int main()
{
Foo("bar");// const char* -> std::string
Foo(1);// int -> double
Other o
Foo(o);// No overload available for this type
}
Is there a way to prioritize implicit conversion to existing specialized templates before creating a new templated function? Or perhaps is there an entirely different way I should be approaching this problem?