Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A workable scheme for "template parameter-conditional compilation"?

Reply
Thread Tools

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

 
 
Lionel B
Guest
Posts: n/a
 
      07-25-2008
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
 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      07-25-2008
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
 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      07-25-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Workable keyboard/mouse/PDF printer =?Utf-8?B?aHB1?= Windows 64bit 1 10-03-2005 07:12 PM
fast universal compression scheme and its implementation in VHDL Jens Mander VHDL 2 09-01-2005 10:08 PM
fast universal compression scheme and its implementation in VHDL Jens Mander VHDL 0 06-10-2005 06:28 AM
how to define javascript workable across browsers particularly innertext Manu Ashok Javascript 1 04-02-2004 02:59 PM
Ok, I downloaded. Now how do I combine them to a workable program? Deerfern Computer Support 3 09-02-2003 02:49 AM



Advertisments