Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - What is the purpose of function template specialization? When to use it?

Tags:

c++

templates

Learning C++, came upon function templates. The chapter mentioned template specialization.

  1. template <> void foo<int>(int);

  2. void foo( int );

Why specialize when you can use the second? I thought templates were suppose to generalize. What's the point of specializing a function for a specific data type when you can just use a regular function?

Obviously, template specialization exists for a reason. When should it be used? I read Sutter's "Why not Specialize..." article but I need more of a layman's version since I'm just learning this stuff.

like image 293
ShrimpCrackers Avatar asked Sep 11 '10 20:09

ShrimpCrackers


People also ask

What is template specialization used for?

This is called template specialization. Template allows us to define generic classes and generic functions and thus provide support for generic programming. Generic programming is an approach where generic data types are used as parameters in algorithms so that they work for variety of suitable data types.

What is the main advantage of using template functions?

Some of the advantages of using templates are: Templates simplify the creation of documents. Templates can ease our workload and make us feel less stressed, and, at the same time, they increase efficiency. Templates increase the attention of the audience.

What are template functions why they are used in C++? Explain briefly?

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Why do we use templates in C?

Templates are an important part of C++, as already mentioned, they allow you to develop functions or Classes that are type generic. You specify the type when you use them. You really should learn templates if, for no other reason, to understand boost and the standard template libraries.


1 Answers

The main difference is that in the first case you are providing the compiler with an implementation for the particular type, while in the second you are providing an unrelated non-templated function.

If you always let the compiler infer the types, non-templated functions will be preferred by the compiler over a template, and the compiler will call the free function instead of the template, so providing a non-templated function that matches the arguments will have the same effect of specializations in most cases.

On the other hand, if at any place you provide the template argument (instead of letting the compiler infer), then it will just call the generic template and probably produce unexpected results:

template <typename T> void f(T) { 
   std::cout << "generic" << std::endl; 
}
void f(int) { 
   std::cout << "f(int)" << std::endl; 
}
int main() {
   int x = 0;
   double d = 0.0;
   f(d); // generic
   f(x); // f(int)
   f<int>(x); // generic !! maybe not what you want
   f<int>(d); // generic (same as above)
}

If you had provided an specialization for int of the template, the last two calls would call that specialization and not the generic template.

like image 72
David Rodríguez - dribeas Avatar answered Nov 14 '22 22:11

David Rodríguez - dribeas