Templates for code that is similar but not identical?

I have a bunch of functions that read completely identical, with the exception of one line of code, which differs depending on the type of input parameter.

Example:

void Func(std::vector<int> input) { DoSomethingGeneral1(); ... DoSomethingSpecialWithStdVector(input); ... DoSomethingGeneral2(); } void Func(int input) { DoSomethingGeneral1(); ... DoSomethingSpecialWithInt(input); ... DoSomethingGeneral2(); } void Func(std::string input) { DoSomethingGeneral1(); ... DoSomethingSpecialWithStdString(input); ... DoSomethingGeneral2(); } 

I wonder how I could avoid this duplication using the template engine. If I understand "specialization" correctly, does he avoid having to code specialized functions twice?

+6
source share
5 answers

here you go .. changed the parameters to the links to avoid copying + make sure you can use the changed values ​​again in Func ()

 void DoSomethingSpecial( std::vector<int>& input ){} void DoSomethingSpecial( int& input ){} void DoSomethingSpecial( std::string& input ){} template< typename T > void Func( T input ) { DoSomethingGeneral1(); DoSomethingSpecial(input); DoSomethingGeneral2(); } 
+8
source

I believe that there is no need to create templates, you can use function overloading, for example:

 void DoSomethingSpecial(const std::vector<int>& input) { } void DoSomethingSpecial(string s) { } void DoSomethingSpecial(int input) { } template <typename T> void Func(const T& input) { //DoSomethingGeneral1(); DoSomethingSpecial(input); //DoSomethingGeneral2(); } int main() { std::vector<int> a; Func(a); Func("abc"); Func(10); } 
+4
source

The best way is to make Func as a template:

 template<typename T> void Func(T input); 

And reload DoSomethingSpecial...() :

 void DoSomethingSpecial(std::string); void DoSomethingSpecial(int); void DoSomethingSpecial(std::vector<int>); 
+3
source

As a style, you may need to highlight parts that differ from each other as a trivial class of templates that more easily specializes. As an example of this method, consider your favorite implementation of an unordered set pattern (or hash_set). Such implementations require you to specialize in a simple hash_key <T> if no specialization already exists. They do not require you to specialize a full container.

Although your example is simple enough to just specialize the whole function, in general, I would implement Func <T> in general and specialize in DoSomethingSpecial <T> like this:

 template< class T > void DoSomethingSpecial(T &input) { ... } template< class T > void Func(T input) { DoSomethingGeneral1(); ... DoSomethingSpecial(T); ... DoSomethingGeneral2(); } template<> void DoSomethingSpecial(std::string &input) { ... } template<> void DoSomethingSpecial(int &input) { ... } 
+3
source

Declare a generic version, but just define specializations:

 template<class T> void DoSpecificStuff( T& withWhat ); template<> void DoSpecificStuff( int& withWhat) { //implementation } template<> void DoSpecificStuff( std::vector<int>& withWhat) { //implementation } 
+1
source

Source: https://habr.com/ru/post/889845/


All Articles