Full specialization of template member function

P

pascal.cathier

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

Victor Bazarov

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 said:
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
 
R

Rolf Magnus

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>() {}
 
P

pascal.cathier

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 écrit :
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>() {}
 
V

Victor Bazarov

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top