L
Lionel B
I frequently seem to run into the following annoyance regarding template
class specialisation: I have a template class which implements, for a
general template parameter, some basic functionality (i.e. set of
methods). But either:
1) For one (or a few) particular methods, the implementation will be
specialised for many possible template parameters, or
2) For one particular template parameter, one (or a few) methods will
have a specialised implementation (or there may be additional methods).
In both case, the bulk of the functionality doesn't need to be
specialised, but nonetheless needs to be duplicated at source level in
each specialisation.
Now I know I can deal with the above situations by implementing the
common non-specialised functionality in a base class, from which the to-
be-specialised template class derives. However, in practice this often
seems to entail substantial coding overhead, and result in more complex
code; essentially, the extra level of derivation "feels conceptually
bogus".
So I've often thought it would be nice if it were possible to do
something like:
template <typename T>
class A
{
void unspecialised_method_1() {...}
void unspecialised_method_2() {...}
void unspecialised_method_3() {...}
...
void specialised_method() {...} // general case
#if (T == int)
void specialised_method() {...} // specialised for int
#endif
#if (T == double)
void specialised_method() {...} // specialised for double
#endif
...
};
Now I did once implement a workable version of more or less the above
involving, if I recall, conditional inclusion of the same header source
file and macros but it was, frankly, ugly and obscure, if not outright
insane.
So I was wondering if anyone (perhaps the folks at Boost?) have invented
a neater mechanism for achieving "template parameter-conditional
compilation" as outlined above.
Hope this makes sense,
class specialisation: I have a template class which implements, for a
general template parameter, some basic functionality (i.e. set of
methods). But either:
1) For one (or a few) particular methods, the implementation will be
specialised for many possible template parameters, or
2) For one particular template parameter, one (or a few) methods will
have a specialised implementation (or there may be additional methods).
In both case, the bulk of the functionality doesn't need to be
specialised, but nonetheless needs to be duplicated at source level in
each specialisation.
Now I know I can deal with the above situations by implementing the
common non-specialised functionality in a base class, from which the to-
be-specialised template class derives. However, in practice this often
seems to entail substantial coding overhead, and result in more complex
code; essentially, the extra level of derivation "feels conceptually
bogus".
So I've often thought it would be nice if it were possible to do
something like:
template <typename T>
class A
{
void unspecialised_method_1() {...}
void unspecialised_method_2() {...}
void unspecialised_method_3() {...}
...
void specialised_method() {...} // general case
#if (T == int)
void specialised_method() {...} // specialised for int
#endif
#if (T == double)
void specialised_method() {...} // specialised for double
#endif
...
};
Now I did once implement a workable version of more or less the above
involving, if I recall, conditional inclusion of the same header source
file and macros but it was, frankly, ugly and obscure, if not outright
insane.
So I was wondering if anyone (perhaps the folks at Boost?) have invented
a neater mechanism for achieving "template parameter-conditional
compilation" as outlined above.
Hope this makes sense,