473,387 Members | 1,420 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,387 software developers and data experts.

Templates and specialization

Hello

Assume this template class has been defined:

template <typename T>
class A
{
public:
void f();
};

Assume I want to define f only for type int. I found my compiler (VC++7)
accepts all of the following combinations (there are 4) for the explicit
specialization:

// The following is optional.
template <typename T>
void A<T>::f();

// In the following, template <> is optional
template <>
void A<int>::f()
{
}

Are all of the combinations standard-conforming ?
In particular, does the standard allow that in the latter we write
template <> without defining the general case for type T ?
Jul 23 '05 #1
2 1011
Kalle Rutanen wrote:
Assume this template class has been defined:

template <typename T>
class A
{
public:
void f();
};

Assume I want to define f only for type int. I found my compiler (VC++7)
accepts all of the following combinations (there are 4) for the explicit
specialization:

// The following is optional.
template <typename T>
void A<T>::f();

// In the following, template <> is optional
template <>
void A<int>::f()
{
}

Are all of the combinations standard-conforming ?
Depends. You haven't posted the complete program. And the correctness
(well-formedness) of it depends on the order of things occurring in the
translation unit, IIRC.
In particular, does the standard allow that in the latter we write
template <> without defining the general case for type T ?


Not unless A<int> has been defined first.

You may define a member of a class template as if it were part of the
full (explicit) specialisation, and for that you need 'template<>' part.
You cannot define a member of a non-existent class. So, if 'A<int>'
hasn't been defined first, omitting 'template<>' before defining
'A<int>::f' is an error.

V
Jul 23 '05 #2
> Depends. You haven't posted the complete program. And the correctness
(well-formedness) of it depends on the order of things occurring in the
translation unit, IIRC.


The whole program is as written with int main(){return 0;}.
In particular, does the standard allow that in the latter we write
template <> without defining the general case for type T ?


Not unless A<int> has been defined first.

You may define a member of a class template as if it were part of the
full (explicit) specialisation, and for that you need 'template<>' part.
You cannot define a member of a non-existent class. So, if 'A<int>'
hasn't been defined first, omitting 'template<>' before defining
'A<int>::f' is an error.


Okay, then it seems VC++7 is not conforming on this... Well, anyway,
this is just for theoretical interest.

Thanks:)
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
12
by: Simon | last post by:
Hi, I'm having a problem with templates and specialisation. I'm using it to overload the same function so it can return different things. I can't see what I'm doing wrong, although my compiler...
6
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
4
by: SainTiss | last post by:
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...
2
by: Shekhar | last post by:
template<typename T> struct A{}; //line 1 template<typename T> struct B{}; //line 2 template<typename T> struct B<A<T> > {}; //line 3: partial specialization of B VC6.0 compiler results for the...
6
by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally...
6
by: vch | last post by:
When defining templates, can I cound on the compiler to parse only those templates that are actually used? For example, in the following definitions: <code> template <class T> struct Conv...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.