Andrey Tarasevich
11/6/2008 7:57:00 AM
Victor Bazarov wrote:
> (2b|!2b)==? wrote:
>> I would like to know if I can specialize only a specific method for a
>> class template.
>>
>> Is the (specialization) code below valid?
>>
>>
>> template <typename T1, typename T2>
>> class MyClass
>> {
>> public:
>> MyClass(const T1&);
>> ~MyClass();
>>
>> //Not implemented inline
>> MyClass * instance(const string& arg1);
>>
>> T1 foo1(T1){ //implementation here ; }
>> T2 foo2(T2){ //implementation here ; }
>> T1 foobar1(T1,T2){ //implementation here ; }
>> T2 foobar2(T2,T1){ //implementation here ; }
>> }
>>
>> //Specialization of only the instance() method
>> template <>
>> MyClass<int, string> * MyClass<int, string>::instance(const string& arg1)
>> {
>> }
>>
>> template <>
>> MyClass<double, double> * MyClass<double, double>::instance(const
>> string& arg1)
>> {
>> }
>
> I don't believe you need the 'template<>'. Each function is not a
> specialisation (because the members are not templates), it's an
> implementation and for that you just indicate for which type you are
> implementing the member function.
> ...
That would be the case if the OP made explicit specializations of the
entire class template
template <> class MyClass<int, string> {
...
MyClass * instance(const string& arg1);
...
}
template <> class MyClass<double, double> {
...
MyClass * instance(const string& arg1);
...
}
and then was providing the definitions for the method 'instance' in each
specialization
MyClass<int, string> *
MyClass<int, string>::instance(const string& arg1)
{
}
MyClass<double, double> *
MyClass<double, double>::instance(const string& arg1)
{
}
But thats' not what the OP is trying to do. If I understood it
correctly, the intent is to provide explicit specializations for the
method only, without introducing explicit specializations for the entire
class template. In that case the 'template<>' portion is required. The
OP's original code is correct.
--
Best regards,
Andrey Tarasevich