By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

specialization of member functions of class templates

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"SainTiss" <st***@gmx.net> wrote...
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
Jul 22 '05 #2

P: n/a
Victor Bazarov wrote:

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

Jul 22 '05 #3

P: n/a
"SainTiss" <st***@gmx.net> wrote...
Victor Bazarov wrote:

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?
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
Jul 22 '05 #4

P: n/a
Victor Bazarov wrote:

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

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


Victor


Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.