Which function gets specialized?

D

desktop

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?
 
J

James Kanze

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*.
 
Z

Zeppe

desktop said:
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
 
Z

Zeppe

James said:
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
 
J

James Kanze

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.)
 
Z

Zeppe

James said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,216
Latest member
DarrelLho

Latest Threads

Top