Templates and specialization

K

Kalle Rutanen

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

Victor Bazarov

Kalle said:
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
 
K

Kalle Rutanen

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;}.
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:)
 

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
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top