Re: On partial specialisation of methods and code duplication
Am 22.12.2010 22:10, schrieb PGK:
I'd like to specialise some methods of an already partially
specialised template class; specialised on 1 of 3 template parameters.
Can I now specialise a subset of methods on a second template
parameter, or must I instead specialise the entire class again?
You cannot partially specialize a function template or a single
member function of a class template in C++.
In the example below, the approach of the Dog struct doesn't work, but
the Cat technique does. The Cat method must duplicate the pi function.
In my actual code I have many functions like pi. If the latter method
is the only way, perhaps I should put the pi-like methods in a parent
class and inherit?
template<typename T1, int I, typename T2> struct Dog;
template<int I, typename T2>
struct Dog<double,I,T2> {
inline int x();
};
template<int I, typename T2>
inline int Dog<double,I,T2>::x() { return I; }
template<typename T2>
inline int Zot<double,1,T2>::x() { return 12345; } // error here
I assume that 'Zot' is supposed to be 'Dog' here.
/* -------------------------------------------- */
template<typename T1, int I, typename T2> struct Cat;
template<int I, typename T2>
struct Cat<double,I,T2> {
inline int x() { return I; }
inline double pi() { return 3.142; }
};
template<typename T2>
struct Cat<double,1,T2> {
inline int x() { return 12345; }
inline double pi() { return 3.142; }
};
This is one way of realizing that. Alternatively you could ensure that Cat<double,I,T2>::x() delegates to a member function of another class template. In this case you only need to partially specialize this invoker class template, e.g.
template <typename T1, int I, typename T2> struct Cat;
template <typename T1, int I, typename T2> struct CatXInvoker;
template <int I, typename T2>
struct Cat<double,I,T2> {
inline int x() { return CatXInvoker<double, I, T2>::_(); }
inline double pi() { return 3.142; }
};
template <int I, typename T2>
struct CatXInvoker<double, I, T2> {
static int _() { return I; }
};
template <typename T2>
struct CatXInvoker<double, 1, T2> {
static int _() { return 12345; }
};
Whether this approach makes sense depends on the complexity of the real class template Cat, though.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]