By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

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

P: n/a
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,

--
Lionel B
Jul 25 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Fri, 25 Jul 2008 11:53:57 +0200, Alf P. Steinbach wrote:
* Lionel B:
>>
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.

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.

--
Lionel B
Jul 25 '08 #2

P: n/a
On Fri, 25 Jul 2008 11:44:23 +0000, Lionel B wrote:
On Fri, 25 Jul 2008 11:53:57 +0200, Alf P. Steinbach wrote:
>* Lionel B:
>>>
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.

Yes, look up enable_if & friends.
Interesting, but probably overkill in my case.
>But generally, you'll most often probably be better off employing other
mechanisms, such as

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

Cheers,
Lionel

--
Lionel B
Jul 25 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.