Re: Partial specialisation
Victor Bazarov wrote:
siddhu wrote:
On May 17, 9:10 am, Alan Woodland <a...@aber.ac.uk> wrote:
[snip]
Is there a valid way to achieve this without using a non member
template function and calling that from within the generic doStuff()?
Thanks,
Alan
You can not specialize the method but you can specialize the class.
Try this.
template <typename T>
class Foo<T,Bar<T> > {
public:
void doStuff();
};
template <typename T>
void Foo<T, Bar<T> >::doStuff() {
std::cout << "Specific" << std::endl;
}
This is usually a problem if the need is to [partially] specialise
a single member function out of dozens. Having to specialise the
entire class template means you need to repeat all but one functions
and give them /exactly same/ implementation as the non-specialised
template.
For reference the solution I went with in the end was:
#include <iostream>
template <typename T>
class Bar {
};
template <typename T, typename P>
class DoStuffImpl {
public:
static void apply() {
std::cout << "General" << std::endl;
}
};
template <typename T>
class DoStuffImpl<T, Bar<T> > {
public:
static void apply() {
std::cout << "Specific" << std::endl;
}
};
template <typename T, typename P>
class Foo {
public:
void doStuff();
};
template <typename T, typename P>
void Foo<T, P>::doStuff() {
DoStuffImpl<T, P>::apply();
}
int main() {
Foo<float, Bar<float> > a;
a.doStuff();
Foo<float, float> b;
b.doStuff();
return 0;
}
Which does what I wanted (i.e. not having to repeat all the other
members) via an additional level of indirection (that probably can be
optimised by a decent compiler anyway)
Thanks,
Alan