By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,107 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,107 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.