473,554 Members | 2,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

So I've often thought it would be nice if it were possible to do
something like:

template <typename T>
class A
void unspecialised_m ethod_1() {...}
void unspecialised_m ethod_2() {...}
void unspecialised_m ethod_3() {...}
void specialised_met hod() {...} // general case
#if (T == int)
void specialised_met hod() {...} // specialised for int
#if (T == double)
void specialised_met hod() {...} // specialised for double

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

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
2 3807
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
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.


Lionel B
Jul 25 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: DJ Majestik | last post by:
OK, I am devising a php page that will handle a form submission, and wanted to know if anyone has already setup such an idea, or if you had links to point to good tutorials on this. Basically I have a form (which I use smarty templating to display, and smartyvalidator to validate). The php page basically is driven by the action variable...
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up zip. Anyhow, after much trial and error I came up with this. This "contains_member" template sets value to 1 if the type D contains a typedef...
by: Lionel B | last post by:
Greetings, I cannot figure out why the following code does not compile: --- BEGIN CODE --- typedef double ftype(double); struct A {
by: Jacky Yuk | last post by:
Hi all, I am new to c++ but using c for long time. Recently, I created a MFC GUI project by VC/C++ 6.0. Everything was fine until I wanted to use "template": template <typename T> class AutoComPtr { ... The following errors were shown:
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since...
by: Ondrej Spanel | last post by:
The code below does not compile with .NET 2003, I get folowing error: w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float' : illegal type for non-type template parameter 'x' The error shows compiler is quite confused - x is not a template parameter at all. I have found two workarounds, but still I think the code should...
by: Brent | last post by:
Like many sites, mine has a standard "look" -- a template, if you will -- that visitors see on each page. I've tried to keep the code and HTML separate to the extent possible, and for most standard page presentations, it works well. However, I have a couple of screen scrape / import routines that can take minutes, even hours, to complete....
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
by: Robbie Hatley | last post by:
I'd always thougth that a C++ compiler/linker should be able to instantiate a template in mulitple places (say, in two different translation units), even using the same template parameters so that the resulting functions or classes are identical, without causing any errors. Or so I thought. But a few days ago, I added a couple of new...
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() { return 0;
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.