473,394 Members | 4,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

specialization of member functions of class templates

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
4 2554
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SainTiss | last post by:
Hi, I've been looking into the standard for a clear statement on whether partial specialization of member functions of class templates is allowed or not. 14.7.3/4 says that explicit...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
5
by: Vaclav Haisman | last post by:
Hi, today a guy came to #C++@IRCNet and during discusion he showed this piece of code: template<bool tswitch=false> class clstmp {
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.