473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.
Jul 23 '05 #1
4 1512
pa************@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>() {}
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
Jul 23 '05 #2
pa************@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() {}
};

template <>
void A::foo<int>() {}

Jul 23 '05 #3

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 :
pa************@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() {}
};

template <>
void A::foo<int>() {}


Jul 23 '05 #4
pa************@gmail.com 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
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
2
by: Jonathan Turkanis | last post by:
Hi All, The simple test program at the end of this message defines a class template foo with a member function bar implemented by delegating to a static member function of a specialization of...
6
by: Kalle Rutanen | last post by:
Here is a short code. I can't see why the template specialization does not work (vsnet2003)? Does the standard allow these kind of specializations? template <int i> class B {}; template...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
10
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.