By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

class template - specialisation for a specific method

P: n/a
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)
{
}
Nov 5 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
(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
Nov 5 '08 #2

P: n/a
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 :/
Nov 5 '08 #3

P: n/a
(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
Nov 6 '08 #4

P: n/a
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
Nov 6 '08 #5

P: n/a
(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
Nov 6 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.