| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
14 Jun 2005 08:58:28 AM |
| Object: |
Full specialization of template member function |
Is it possible to fully specialize a template member function? If not,
why is that so? The following code fails to compile with GCC:
class A
{
public:
template < typename T >
void foo() {}
template <>
void foo<int>() {}
};
int main(int argc, char * argv[])
{
A a;
a.foo<void>();
a.foo<int>();
}
I can still wrap these functions 'foo' as the operator() of a template
class that I can then specialize to achieve the same goal, but I would
rather not if I can avoid to.
Thanks
B.
.
|
|
| User: "Rolf Magnus" |
|
| Title: Re: Full specialization of template member function |
14 Jun 2005 09:08:44 AM |
|
|
wrote:
Is it possible to fully specialize a template member function?
If not, why is that so? The following code fails to compile with GCC:
class A
{
public:
template < typename T >
void foo() {}
template <>
void foo<int>() {}
};
int main(int argc, char * argv[])
{
A a;
a.foo<void>();
a.foo<int>();
}
I can still wrap these functions 'foo' as the operator() of a template
class that I can then specialize to achieve the same goal, but I would
rather not if I can avoid to.
Try:
class A
{
public:
template < typename T >
void foo() {}
};
template <>
void A::foo<int>() {}
.
|
|
|
| User: "" |
|
| Title: Re: Full specialization of template member function |
15 Jun 2005 06:20:46 AM |
|
|
Thanks... Is there a way to do the same thing with a templated class
though? It seems that the following does not work:
template < typename U >
class A
{
public:
template < typename T >
void foo();
};
template < typename U >
template < typename T >
void A<U>::foo() {}
template < typename U >
template <>
void A<U>::foo<int>() {}
Rolf Magnus a =E9crit :
pascal.cathier@gmail.com wrote:
Is it possible to fully specialize a template member function?
If not, why is that so? The following code fails to compile with GCC:
class A
{
public:
template < typename T >
void foo() {}
template <>
void foo<int>() {}
};
int main(int argc, char * argv[])
{
A a;
a.foo<void>();
a.foo<int>();
}
I can still wrap these functions 'foo' as the operator() of a template
class that I can then specialize to achieve the same goal, but I would
rather not if I can avoid to.
Try:
class A
{
public:
template < typename T >
void foo() {}
};
=20
template <>
void A::foo<int>() {}
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Full specialization of template member function |
15 Jun 2005 08:22:32 AM |
|
|
wrote:
Thanks... Is there a way to do the same thing with a templated class
though? It seems that the following does not work:
template < typename U >
class A
{
public:
template < typename T >
void foo();
};
template < typename U >
template < typename T >
void A<U>::foo() {}
template < typename U >
template <>
void A<U>::foo<int>() {}
Please don't top-post.
To answer your question, no, to specialise a member of a template class
you need to first specialise the class itself. It's a limitation of the
language.
Get a good book on templates. I suggest "C++ Templates" by Vandevoorde
and Josuttis.
V
.
|
|
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Full specialization of template member function |
14 Jun 2005 09:01:28 AM |
|
|
wrote:
Is it possible to fully specialize a template member function? If not,
why is that so? The following code fails to compile with GCC:
class A
{
public:
template < typename T >
void foo() {}
template <>
void foo<int>() {}
I believe the specialisation has to be defined outside the class.
};
Move it here:
template <>
void A::foo<int>() {}
int main(int argc, char * argv[])
{
A a;
a.foo<void>();
a.foo<int>();
}
I can still wrap these functions 'foo' as the operator() of a template
class that I can then specialize to achieve the same goal, but I would
rather not if I can avoid to.
V
.
|
|
|
|

|
Related Articles |
|
|