tags:

views:

83

answers:

3

While reading this, I'm confused with the following examples:

// Example 2: Explicit specialization 
// 
template<class T> // (a) a base template 
void f( T );

template<class T> // (b) a second base template, overloads (a) 
void f( T* );     //     (function templates can't be partially 
                  //     specialized; they overload instead)

template<>        // (c) explicit specialization of (b) 
void f<>(int*);

// ...

int *p; 
f( p );           // calls (c)

Here, (c) is explicit specialization of (b).

// Example 3: The Dimov/Abrahams Example 
// 
template<class T> // (a) same old base template as before 
void f( T );

template<>        // (c) explicit specialization, this time of (a)
void f<>(int*);

template<class T> // (b) a second base template, overloads (a) 
void f( T* );

// ...

int *p; 
f( p );           // calls (b)! overload resolution ignores 
                  // specializations and operates on the base 
                  // function templates only

Here (c) is explicit specialization of (a). Why is that? Is this because of the ordering of the declaration?

Thanks in advanced.

+6  A: 

Yes, it's because of the ordering of the declaration. When the compiler encounters (c) in the second set, the only defined template to specialize is (a).

This is why you must be careful in ordering your template specializations.

The C++ Programming Language goes into quite some detail about this (Section 13.5.1). I highly recommend it.

JoshD
+6  A: 

It's the order but it's not only the order. For the first code, both templates was defined before. But the second was taken.

This is because the two templates are compared and it is found that (T) matches any type that (T*) matches, but that (T*) does not match all types that (T) matches. Thus, the second template is more specialized. Whenever you put an explicit specialization and those two templates match, the templates are compared and the more specialized one is associated by the explicit specialization. This comparison is called "partial ordering".

Johannes Schaub - litb
+5  A: 

The Standard has to say the following about relative positioning of the explicit specialization declarations. [Section 14.7.3]

The placement of explicit specialization declarations for function templates, class templates, member functions of class templates, static data members of class templates, member classes of class templates, member class templates of class templates, member function templates of class templates, member functions of member templates of class templates, member functions of member templates of non-template classes, member function templates of member classes of class templates, etc., and the placement of partial specialization declarations of class templates, member class templates of non-template classes, member class templates of class templates, etc., can affect whether a program is well-formed according to the relative positioning of the explicit specialization declarations and their points of instantiation in the translation unit as specified above and below. When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

Prasoon Saurav