[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

class template - specialisation for a specific method

(2b|!2b)==?

11/5/2008 4:23:00 PM

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)
{
}
5 Answers

Victor Bazarov

11/5/2008 4:30:00 PM

0

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

(2b|!2b)==?

11/5/2008 4:47:00 PM

0

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

Thanks Victor,

Indeed, it would appear that you are correct. Not adding the template<>
also gets rid of my compilation errors. It seems a lot of the
documentation out there on template specialization are using incorrect
terminology + examples :/

Andrey Tarasevich

11/6/2008 7:52:00 AM

0

(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?
> ...
> //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)
> {
> }

Yes, it is valid. And yes, you do need the 'template<>' bit when you
perform explicit specialization. What you are trying to do is
illustrated exactly by an example in 14.7/17 in the language standard.

--
Best regards,
Andrey Tarasevich

Andrey Tarasevich

11/6/2008 7:57:00 AM

0

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

Andrey Tarasevich

11/6/2008 8:01:00 AM

0

(2b|!2b)==? wrote:
>
> Indeed, it would appear that you are correct. Not adding the template<>
> also gets rid of my compilation errors. It seems a lot of the
> documentation out there on template specialization are using incorrect
> terminology + examples :/

Are you sure you are posting the same code you are trying to compile?
Because in the conforming compiler it should be the other way around:
your original code should compile fine (aside from teh missing ';' and
other unrelated problems), while removing the 'template<>' part from the
method specialization should produce compiler errors.

--
Best regards,
Andrey Tarasevich