There is a function, the implementation of which does not differ with any data types. But the data type can not be absolutely anything (of all the possible data types that the function will handle is 9).

The question is: is it worth making a template for such a function, and inside it is just to check whether the data type is "correct", or is it better to do everything with a normal overload?

  • And what are these 9 types? How do they differ from the "wrong"? - Voidificator
  • @Voidificator standard types int, double, bool, char and all modified, which can easily turn into std :: string. If, for example, you insert a custom class into a function, it will not turn into a string. - Samilton
  • I recommend to look at the mechanism of explicit instantiation . It will also allow you to avoid duplication of code, and solve problems with slowing down the compilation (place the code of those template specializations that you need, where you need it). - Constructor

1 answer 1

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.

  • About std::enable_if everything is clear, thanks. And what about std::is_convertible , it doesn't seem to apply here. Because when executing std::is_convertible<int, std::string>::value , the result will be false . - Samilton
  • @Samilton, can you attach a part of your code to the question, what actions do you perform the same for the types you specified, as for std :: string? - Jenssen
  • The @Samilton thing is that the “numbers” and “strings” in principle are different types (logically), and the fact that we do not expect a normal reduction of the first to the second is normal. Therefore, we use character-by-character translation, recognize the digits of the number, build the string through ASCII codes. As if the number is encoded by digit characters, which we translate into characters in the string. In this situation, you should make an explicit recognition of this situation in the inclusion, for example, through std :: is_integral. - Jenssen
  • this is understandable, I just thought that when checking for conversion std :: string the std functions would be involved. For example, to convert double, int to string, I use std :: to_string (), and it creates a string of these types without any problems. - Samilton
  • one
    @Samilton agrees, std :: is_convertible is more suitable for hierarchies and built-in types, it does not look in your task. Then use std :: is_integral, etc. en.cppreference.com/w/cpp/types/is_integral - there are some more similar verification operations at the bottom of the page. - Jenssen