0

Here is a simple example:

#include <iostream>

template <class T>
void print(T in){ 
  printf("NOT printing int\n"); 
}

template <> // <-- template specialization
void print<int>(int in){ 
  printf("printing int\n"); 
}

int main(){
 print(5); // printing int
 return 0;
}

But the int version can be written without even using templates, like this:

void print(int in){ 
  printf("printing int\n"); 
}

So what the point of template specialization here?

Edit (example of where it might be needed)

#include <iostream>

template <class T>
void print(){ 
  printf("printing float\n"); 
}

template <> 
void print<int>(){ 
  printf("printing int\n"); 
}

int main(){
 print<float>();
 return 0;
}
Dan
  • 577
  • 1
  • 3
  • 9
  • Think about the returnvalue. – Ulrich Eckhardt Sep 25 '20 at 16:31
  • For functions, overloads are generally preferred over specialization indeed. but there is also specialization for classes (such as traits). – Jarod42 Sep 25 '20 at 16:32
  • 3
    I'm not sure what your question is. Let's say that template specialization is not useful in this particular case. It could still be useful in other places. – cigien Sep 25 '20 at 16:33
  • Such templates could still be conveniently used in other ones, which only pass type parameters through. – πάντα ῥεῖ Sep 25 '20 at 16:34
  • could you give an example of the other places? – Dan Sep 25 '20 at 16:34
  • I believe I just did. – πάντα ῥεῖ Sep 25 '20 at 16:35
  • When selecting a template instantiation the non-specialized version won't be considered. – Eljay Sep 25 '20 at 16:40
  • Now try to write a recursive template class. Like, oh, std::tuple. Without specialization. – Sam Varshavchik Sep 25 '20 at 16:40
  • @SamVarshavchik: `std::tuple` doesn't require recursion AFAIR. – Jarod42 Sep 25 '20 at 16:44
  • There are *many, many* uses of template specializations. The question is way too broad at the moment. – cigien Sep 25 '20 at 16:45
  • I would be very curious to see your compiler's header files' implementation of std::tuple (it's all there in the header files) that does not use recursion and a specialization for std::tuple<>, @jarod42 – Sam Varshavchik Sep 25 '20 at 16:49
  • *Function* template specialisation is often advised against by C++ gurus. Class template specialisation is a totally different matter. – n. m. could be an AI Sep 25 '20 at 16:51
  • @SamVarshavchik: [why-is-it-not-good-to-use-recursive-inheritance-for-stdtuple-implementations](https://stackoverflow.com/questions/9641699/why-is-it-not-good-to-use-recursive-inheritance-for-stdtuple-implementations) provides [link](http://llvm.org/svn/llvm-project/libcxx/trunk/include/tuple) to implementation. After a quick view, there is a specialization for empty tuple though, but I don't see recursion. – Jarod42 Sep 25 '20 at 17:20
  • @Jarod42 tuple's Base_T inherits from tuple_impl, which inherits from `public __tuple_leaf<_Indx, _Tp>...`, the recursion is in setting up this parameter pack. The template itself is not recursive, but its class member gets constructed with the help of a recursive helper tempalte. I repeat: good luck implementing `std::tuple` without recursion. – Sam Varshavchik Sep 25 '20 at 22:07
  • @SamVarshavchik: compiler might provide intrinsic for `index_sequence` (to avoid recursion and so have only 1 instantiation instead of naive `O(N)` or better `O(log(N))` depth recursion. But anyway `tuple`/`index_sequence` are not recursive per se. – Jarod42 Nov 01 '20 at 01:30

1 Answers1

1

The point is that you cannot do everything with overloads. Consider for example a case where there is no argument:

template<class T>
void foo();

There is no way to overload this function because there are no arguments. But it can be called with explicit instantiation:

foo<int>();

And those instantiations can be specialised.

Then there are class template specialisations which of course cannot be done using function overloading.


There is some overlap between what can be done with overloads and what can be done with template specialisation. It's typically considered better to use an overload when it is an option.

eerorika
  • 232,697
  • 12
  • 197
  • 326