469,156 Members | 2,152 Online

# Which function gets specialized?

I have this example:

template<class T(1)
void f( T );

template<class T(2)
void f( T* );
template< (3)
void f<>(int*);
Which of (1) and (2) does (3) specialize, and why?

Is the order just a matter of when the specialization is declared?
May 12 '07 #1
5 1369
desktop wrote:
I have this example:

template<class T(1)
void f( T );

template<class T(2)
void f( T* );
template< (3)
void f<>(int*);
Which of (1) and (2) does (3) specialize, and why?

Is the order just a matter of when the specialization is declared?
It's simpler: (2) is a partial specialization of (1), while (3) is a
specialization of (1). Note that there is no relationship between the
specializations, they are just different implementations. So, it makes
no sense to ask if (3) specializes directly (1) or the partial
specialization (2): it's just a specialization of (1).

Regards,

Zeppe
May 12 '07 #2
On May 13, 12:18 am, Zeppe
<z...@remove.all.this.long.comment.email.itwrote :
desktop wrote:
I have this example:
template<class T(1)
void f( T );
template<class T(2)
void f( T* );
template< (3)
void f<>(int*);
Which of (1) and (2) does (3) specialize, and why?
Is the order just a matter of when the specialization is declared?
It's simpler: (2) is a partial specialization of (1), while (3) is a
specialization of (1). Note that there is no relationship between the
specializations, they are just different implementations. So, it makes
no sense to ask if (3) specializes directly (1) or the partial
specialization (2): it's just a specialization of (1).
There's no such thing as partial specialization of a function
template. (1) and (2) are two, independent function templates.

Partial ordering of function templates (§14.5.5.2) says that the
specialization (3) is for function template (2). This makes
sense, because the same partial ordering is used in overload
resolution, which means that in fact, (1) will never be called
with an int*.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 12 '07 #3
James Kanze wrote:
On May 13, 12:18 am, Zeppe
<z...@remove.all.this.long.comment.email.itwrote :
>desktop wrote:
>>I have this example:
>>template<class T(1)
void f( T );
>>template<class T(2)
void f( T* );
>>template< (3)
void f<>(int*);
>>Which of (1) and (2) does (3) specialize, and why?
>>Is the order just a matter of when the specialization is declared?
>It's simpler: (2) is a partial specialization of (1), while (3) is a
specialization of (1). Note that there is no relationship between the
specializations, they are just different implementations. So, it makes
no sense to ask if (3) specializes directly (1) or the partial
specialization (2): it's just a specialization of (1).

There's no such thing as partial specialization of a function
template. (1) and (2) are two, independent function templates.
Whatever. The effect is that for a pointer (2) will be called instead of
(1), and, if there is no method to call (1) with pointer, it's the same
of a partial specialization, isn't it?

Regards,

Zeppe
May 12 '07 #4
On May 13, 1:24 am, Zeppe <z...@remove.all.this.long.comment.email.it>
wrote:
James Kanze wrote:
On May 13, 12:18 am, Zeppe
<z...@remove.all.this.long.comment.email.itwrote :
desktop wrote:
I have this example:
>template<class T(1)
void f( T );
>template<class T(2)
void f( T* );
>template< (3)
void f<>(int*);
>Which of (1) and (2) does (3) specialize, and why?
>Is the order just a matter of when the specialization is declared?
It's simpler: (2) is a partial specialization of (1), while (3) is a
specialization of (1). Note that there is no relationship between the
specializations, they are just different implementations. So, it makes
no sense to ask if (3) specializes directly (1) or the partial
specialization (2): it's just a specialization of (1).
There's no such thing as partial specialization of a function
template. (1) and (2) are two, independent function templates.
Whatever. The effect is that for a pointer (2) will be called instead of
(1), and, if there is no method to call (1) with pointer, it's the same
of a partial specialization, isn't it?
No. In this case, it sort of acts like one, but partial
specialization of class templates and overloading of function
templates obey different rules in general, and it is better to
keep the vocabulary clean, and to not mix up the two.

You might want to reread section 13.7 of Vandevoorde and
Josuttis. The section carries the somewhat misleading title of
"Partial Specialization of Function Templates", but it explains
in detail the concrete differences between partial
specialization (of class templates) and overloading (of function
templates). (If you haven't already read the book, then you
definitly should. It is the definitive reference concerning
templates, and it is exceptionally well written.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 13 '07 #5
James Kanze wrote:
You might want to reread section 13.7 of Vandevoorde and
Josuttis. The section carries the somewhat misleading title of
"Partial Specialization of Function Templates", but it explains
in detail the concrete differences between partial
specialization (of class templates) and overloading (of function
templates).
I see. Really interesting reference, thanks for the suggestion!

Regards,

Zeppe
May 13 '07 #6

### This discussion thread is closed

Replies have been disabled for this discussion.