Kalle said:
Hello, John and Ferdi.
It is indeed possible to specialize the class in the way Ferdi
mentioned. I am surprised too see the given situation really is
forbidden. "C++ in a nutshell", page 195: "You cannot partially
specialize a member of a class template."
But we can still explicitly specialize:
template <>
void A<B<1> >::f()
{
}
Can you think of a reason why this decision was made?
You can't partially specialize functions. Instead you have overloading,
which is very powerful. Just remember: functions -> overloading, class
-> specialization. (Explicitly) specializing functions can also cause
some troubles when overloading is used as well. Prefer overloading.
My intention is to convert pixel color formats to each other using
template meta programming (for example ARGB8888 <-> ARGB1555). You can
see the actual code here:
http://kaba.hilvi.org/project/convert/
Look at imageformatconvert.h and imageformatconvert.inl. There (.h) I
had to explicitly specialize the ImageFormatConvert template class three
times (like Ferdi suggested), while the code for them is practically
identical.
You could use overloading with a tag type in your original example.
Ie. something like this (untested, top of my head):
template <int i>
struct B{};
struct default_tag {};
struct special_tag {};
template <typename T>
struct select_conversion {
typedef default_tag tag;
};
template <int i>
struct select_conversion<B<i> > {
typedef special_tag tag;
};
template <typename T>
class A {
public:
void f() {
// do common stuff
// do type specific stuff
f_(typename select_conversion<T>::tag());
}
private:
// select on overloading tag type
void f_(const default_tag& x) {}
void f_(const special_tag& x) {}
};
A<int> ai;
A<B<2> > ab;
ai.f();
ab.f();
Or something similar.
--
Regards,
Ferdi Smit (M.Sc.)
Email: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands