   Templates C++
  First off, whats the difference between the following? I understhand that they are both specialisations, but how do they differ?template<>void OutInt( int ){//...}template<>void OutInt<int>( int ){//...}And secondly, how come these to apps produce different results? As far as i can tell its only the order that has changed. The code is from Guru of the week, by herb Sutter. Where he is talking about how function specialisation works. From what i understand.- Function templates dont partially specialize, only overload- If a normal function is found, dont use templates- Function template specializations are only considered *after* a non specialised function template is found, and then only if a specialisation better matches the calling code, will it get used.// Example 2: Explicit specialization // template<class T> void f( T ){cout << "void f( T ) : ";}template<class T>  void f( T* ){cout << "void f( T* ) : ";}template<>  void f<>(int*){cout << "void f<>(int*) : ";}int main(){int b;int *p = &b; f( p ); getchar();}// Example 3: The Dimov/Abrahams Example // template<class T> void f( T ){cout << "void f( T ) : ";}template<>  void f<>(int*){cout << "void f<>(int*) : ";}template<class T> void f( T* ){cout << "void f( T* ) : ";}int main(){int b;int *p = &b; f( p ); getchar();}
Quote:First off, whats the difference between the following? I understhand that they are both specialisations, but how do they differ?template<>void OutInt( int ){//...}template<>void OutInt<int>( int ){//...}They don't.Note the first syntax isn't always possible, however.Quote:And secondly, how come these to apps produce different results? // Example 2: Explicit specialization // template<class T> void f( T ){cout << "void f( T ) : ";}template<class T>  void f( T* ){cout << "void f( T* ) : ";}template<>  void f<>(int*){cout << "void f<>(int*) : ";}int main(){int b;int *p = &b f( p ); getchar();}// Example 3: The Dimov/Abrahams Example // template<class T> void f( T ){cout << "void f( T ) : ";}template<>  void f<>(int*){cout << "void f<>(int*) : ";}template<class T> void f( T* ){cout << "void f( T* ) : ";}int main(){int b;int *p = &b f( p ); getchar();}Because in the first case, the specialization is actually specializing the T* overload, while in the other it is specializing the T overload.[Edited by - loufoque on June 28, 2009 5:15:46 PM];  Could yo explain any further?Would it not first match the call to:template<class T> void f( T* )and then look for a specialisation of that, thus finding the specialied f?   ;  Best match between T and T* is T*.Then the specializations of T* are considered, if there is any. There is only one in the first case, int*, which is an even better match.Therefore the first matches int*, the other matches T*.   ;Quote:Because in the first case, the specialization is actually specializing the T* overload, while in the other it is specializing the T overload.This is the answer. In the second case, you've defined the specialization before the T* overload even exists, so obviously it must be specializing the T overload. T* is a better match than T however, so it never even looks for the specialization in the second case.   