473,406 Members | 2,371 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,406 software developers and data experts.

How to partially specialize a class but NOT specialize a member function:

Ok, I've got a class with two template parameters (A and B), and a
member function foo(). I want to specialize foo for a particular class
A. Is this possible? The following code shows an example:

=====================================
template<typename A, typename B>
class Foo
{
int foo() { return 5; }
};
template<typename B>
int Foo<int, B>::foo() { return 6; }

int main()
{
return 0;
}
======================================

The above example doesn't compile; g++ reports the following error:

test.cpp:11: error: no `int Foo<int, B>::foo()' member function
declared in class `Foo<int, B>'
test.cpp:11: error: template definition of non-template `int Foo<int,
B>::foo()'

I take it this means I can't write function implementations for class
specializations if the class specialization hasn't been declared...?

Thanks,
--Steve

Aug 2 '05 #1
1 1372
mrstephengross wrote:
Ok, I've got a class with two template parameters (A and B), and a
member function foo(). I want to specialize foo for a particular class
A. Is this possible?
Yes, as soon as you specialise your class template for that particular
class A.
The following code shows an example:

=====================================
template<typename A, typename B>
class Foo
{
int foo() { return 5; }
};
template<typename B>
int Foo<int, B>::foo() { return 6; }

int main()
{
return 0;
}
======================================

The above example doesn't compile; g++ reports the following error:

test.cpp:11: error: no `int Foo<int, B>::foo()' member function
declared in class `Foo<int, B>'
test.cpp:11: error: template definition of non-template `int Foo<int,
B>::foo()'

I take it this means I can't write function implementations for class
specializations if the class specialization hasn't been declared...?


Yes. To define a member you need to define the class first, of which the
function is a member.

V
Aug 2 '05 #2

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

Similar topics

4
by: Old Wolf | last post by:
I have a template class like this: template<typename T, int N> struct Foo { ........... }; which I have successfully specialized for some types, eg:
3
by: PengYu.UT | last post by:
I have the following two program. The first one compiles well, but the second one doesn't. The only difference between them is one more template parameter is added for the second program. Would...
2
by: Imre | last post by:
Hi I'm trying to write a function template called CallF(), that has a type argument called T. It has one argument, a T*. If T has a member function F(), then CallF() should call it, otherwise it...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
7
by: mathieu | last post by:
Hi there, I know this is not possible in c++. So my question, how should I rewrite the following piece of code (without using a dummy class which template parameter could be use for partial...
5
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.