   Template Specialisation
  Concerning//cout type Ttemplate < typename T >void Function(T t){std::cout << "template < T > void Function(T) : " << t << "\n";}//specialize for pointer type Ttemplate < typename T >void Function(T* t){std::cout << "template < T > void Function(T*) : " << t << "\n";}//specialize for type inttemplate <>void Function< int >( int T){std::cout << "template < int > void Function(int T) : " << T << "\n";}//specialize for type int*template <>void Function< int* >( int* T ){std::cout << "template < int* > void Function(int* T) : " << T << "\n";}What does the following do then?template <>void Function< int >( int* T ){std::cout << "template < int* > void Function(int* T) : " << T << "\n";}//ortemplate <>void Function< int* >( int T ){std::cout << "template < int* > void Function(int* T) : " << T << "\n";}   //wont compile thoughAlso, why specialise function templates? whats the difference between that and just having an explicit function?   
  Template specialization is just a means to supply a special case of code when the template parameter is of a specific type, hence the name "template specialization".It is useful because it allows one to develop specially-optimized routines when the type allows, while also having a general-purpose fallback for other types. One example is a mathematical matrix/vector classes -- SIMD instruction sets like SSE or Altivec are perfect for optimizing many operations, but the series of instructions is different depending on whether the elements are float, double, or some other type.What you gain over separate, explicit member functions is that it expresses the fact that the specialization is just a special case of a specific template (rather than a distinct-though-similar entity) and it also allows you to re-use the default implementations of some methods which may not be optimizeable (or worth the effort), thereby reducing code duplication.   ;  This is a dark corner of the C++ language that I don't quite understand, but I thought I would point you to this page about it.   ;Quote:Original post by maya18222Also, why specialise function templates? whats the difference between that and just having an explicit function?Hello,Sometimes you want to specialize a template for a particular type, suppose you have the following function:template <class T>bool comp(T a,T b){ return (a<b);}If you want this to work for char * you have to specialize it for that. There are numerous cases like this , it is not `why`  but `when` you have to specialize a template, because you can't build generic stuff to work with everything, sometimes you have to specialize it in order to work with all types.As for your example i think it will not compile because you have already specialized the template for int and int *Raxvan   ;Quote:Original post by maya18222Concerning*** Source Snippet Removed ***What does the following do then?*** Source Snippet Removed ***Also, why specialise function templates? whats the difference between that and just having an explicit function?It's difficult to know why it doesn't compile without the errors, but I am also confused about this block://specialize for pointer type Ttemplate < typename T >;void Function(T* t){std::cout << "template < T > void Function(T*) : " << t <<; "\n";}Why bother specializing here? The body of the function is exactly the same as abouve except for T*.  Is it just so that the text can read Function(T*)? Calling << on t in that specialisation will dump the pointer value of t, not t itself. Usually people do specialization of T* when they need to dereference the pointer - which is a perfect example for when to use specialization.Consider an Object with a Print() function. We have a generic way of calling it with your method Function():template <typename T>;void Function(T t) { t.Print(); }Object anObject;Function<Object>;( anObject ); // will call anObject.Print();But consider using it with a pointer to AnObject:Object* pObject = new Object();Function<Object>( pObject ); // will call pObject.Print() - ERROR - should be pObject->Print() or (*pObject).Print()In the above code, pObject.Print() is expanded from the template instead of pObject->Print(). The compilation will fail. How to solve? With a specialization:template<>void Function(T* t); { (*t).Print(); } // correct calling method or t->Object* pObject = new Object();Function<Object>;( pObject ); // will call pObject->Print() Furthermore, consider a case where, for all types T Print takes no argument, except for ONE case:class MySuperObject {void Print( bool verbose );}template<>;void Function(MySuperObject* t) { t->Print( true ); }I would think your sample instead would want//specialize for pointer type Ttemplate < typename T >void Function(T* t){std::cout << "template < T > void Function(T*) : " << (*t) << "\n";}I also think that you always want to be consistent with your TYPE versus argument. In your examples, the template definition uses a capital T for the typename, while the variable is a lower case t - this is great. But in the specializations you move to using a capital T for the variable. In the long run, when writing large template code bases, you want to be consistent. When doing specializations it is common to use the same variable name so it would be template <typename T>;void Function( T t ) { << t }template<>void Function( T* t ) { << (*t) }template<>void Function( int t ) { << t }template<>void Function( int* t ) { << (*t) }Hope that makes some sense.S.   