Specialized Templates - Aim

I know what templates in C ++ do, but today I saw some strange piece of code:

template <> void swap(foo &a, foo &b) { a.name = b.name; a.id = 1; // blah blah blah } 

I did a little research and found out that it is called a specialized template or something similar.

If in the above code I remove the template <> , I get exactly the same results. There are also no generic types, for example, in the regular template function ...

My question is: what is the purpose of using them?

+4
source share
1 answer

Removing template <> from this function will result in the following changes:

  • This will not be a template specialization.
  • It would not receive a call from the swap<foo>( a, b ) code
  • This might be the best match for swap( a, b ) when a and / or b are types that can be converted to foo .
+3
source

All Articles