specialization of member functions of class templates

S

SainTiss

Hi,

From what I've read in several places, it seems that explicit specialization
of member functions of class templates is allowed, but partial
specialization isn't:

template<class T, class R> class A {
void foo();
}

template <>
void A<int,double>::foo() {} // allowed

template<class T>
void A<T,double>::foo() {} // forbidden



However, when looking for justification in the ISO standard (final draft,
1996), it seems the first isn't explicitly allowed in there, and neither is
the second explicitly forbidden, even though the first seems to be used in
an example in 14.5.4.3

So does anyone know what the story is then? Does the standard really
allow/forbid this or not?

Thanks,

Hans
 
V

Victor Bazarov

SainTiss said:
From what I've read in several places, it seems that explicit specialization
of member functions of class templates is allowed, but partial
specialization isn't:

template<class T, class R> class A {
void foo();
}

template <>
void A<int,double>::foo() {} // allowed

template<class T>
void A<T,double>::foo() {} // forbidden

No, not forbidden. You just have to partially specialise the class
template as well, before you attempt to define the member. 14.5.4.3/1.
However, when looking for justification in the ISO standard (final draft,
1996), it seems the first isn't explicitly allowed in there, and neither is
the second explicitly forbidden, even though the first seems to be used in
an example in 14.5.4.3

So does anyone know what the story is then? Does the standard really
allow/forbid this or not?

Both are allowed. A partial specialisation of a member requires
the same partial specialisation of the enclosing template to exist.
At least that's how I read the Standard.

Victor
 
S

SainTiss

Victor said:
No, not forbidden. You just have to partially specialise the class
template as well, before you attempt to define the member. 14.5.4.3/1.

"The template argument list of a member of a class template partial
specialization shall match the template argument list of the class template
partial specialization. A class template specialization is a distinct
template."

Is this why you conclude that the partially specialised class needs to be
defined before its member is defined? That's not really clear, is it?

Additionally, that doesn't explain at all why it *is* allowed for an
explicitly specialised class...

Is the standard really this unclear about this?

Thanks,

Hans
 
V

Victor Bazarov

SainTiss said:
"The template argument list of a member of a class template partial
specialization shall match the template argument list of the class template
partial specialization. A class template specialization is a distinct
template."

Is this why you conclude that the partially specialised class needs to be
defined before its member is defined? That's not really clear, is it?

I am not sure about clarity (or lack thereof). A partial specialisation
is a whole different template, and its members know nothing about the
members of the original template, whereas a full specialisation of
a member _relies_ on the implicit instantiation of the original template.
That would be the main difference, I think.
Additionally, that doesn't explain at all why it *is* allowed for an
explicitly specialised class...

Because a full specialisation causes an implicit instantiation of the
original template (I am still looking for the standard phrasing on that).
Is the standard really this unclear about this?

For more clarification of the Standard and why it's so unclear I strongly
recommend asking in comp.std.c++.

Victor
 
S

SainTiss

Victor said:
I am not sure about clarity (or lack thereof). A partial specialisation
is a whole different template, and its members know nothing about the
members of the original template, whereas a full specialisation of
a member _relies_ on the implicit instantiation of the original template.
That would be the main difference, I think.

Because a full specialisation causes an implicit instantiation of the
original template (I am still looking for the standard phrasing on that).


That would indeed be a satisfactory explanation, if it is backed by the
standard. So I'm looking into that as well, but although it is possible to
interpret certain parts of it in that way, so far I too failed to find a
part which clearly justifies this.

Thanks,

Hans
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top