Full specialization of template member function



 DEVELOP > c-Plus-Plus > Full specialization of template member function

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.


  Page 1 of 1

1

 


Related Articles
Full specialization of a member function template of a class template
separating delcaration and definition of full template specialisation - how?
Rationale behind restrictions in full template specialization?
Download Visual Studio 2005 Full
REQ Full Time Consultant-PLM/CATIA/VPM/ENOVIA/CAA: Long Beach CA
KASPERSKY 2009 Kai 2010 - Antihacker - Corp Total Suite - KISS.2006 - Personal - Professionel CRAC + FULL KasperSky KeyViewer + Keys till 2010.rar
Does any one have C Traps and Pitfalls full edition in softcopy
Use full info and intelligent games and etc.....
Full screen mode
File full of '^@' characters after binary write
Full screen mode
Diggins PDP #1 : Binary Arithmetic Algorithms (division / multiplication / full_adder )
Downloading full page source of a web page
difference between plain C and a full object oriented programming
Recent file list - FULL PATH?
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER