In general, one of the advantages (not the main one, of course) of templates is the reduction of code duplication. And so it is necessary to write 9 almost identical (except for types) functions. Duplication code! (Or are you not a lazy programmer?) Use std::enable_if_t and cut off unnecessary types, for this purpose this mechanism was introduced into the standard. Given the comment about the types that are converted to std::string , you can try using std::is_convertible . Although it is worth considering the specifics of your task, perhaps this is an insufficient condition. Here is an example of using std::is_convertible from the site :
#include <iostream> #include <type_traits> struct A { }; struct B : A { }; int main() { std::cout << std::boolalpha; std::cout << "is_convertible:" << std::endl; std::cout << "int => float: " << std::is_convertible<int,float>::value << std::endl; std::cout << "int = >const int: " << std::is_convertible<int,const int>::value << std::endl; std::cout << "A => B: " << std::is_convertible<A,B>::value << std::endl; std::cout << "B => A: " << std::is_convertible<B,A>::value << std::endl; return 0; }
About SFINAE idiom can be read here . Options for using std::enable_if is here .
If type checking with std::is_convertible not suitable for this task, then type checking can be done based on std::is_integral , std::is_floating_point and other similar structures. You can read about them, for example, here . I suppose in the task more than 9 types can be processed, taking into account all qualifiers, int types with size, different char types, etc. In this case, the use of std::is_integral and the company is optimal, since they successfully handle all such situations.