A workable scheme for "template parameter-conditional compilation"?

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,
 
L

Lionel B

* Lionel B:

Yes, look up enable_if & friends.

But generally, you'll most often probably be better off employing other
mechanisms, such as

* Simple overloading. :)

* Policy template parameters & traits classes (get yourself Andrei's
"Modern C++ Design" if you don't have that book already).

* Even the old mechanism of virtual member functions.

enable_if has a tendency to yield brittle code, and code that must be
tested with at least two different compilers to be reasonably sure that
it will be portable and work as intended.

Thanks for the suggestions, Alf - will investigate.
 
L

Lionel B

Interesting, but probably overkill in my case.

In fact it seems simple overloading (+ SFINAE) will generally do the job.

Cheers,
Lionel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top