473,729 Members | 2,355 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
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_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
#endif
#if (T == double)
void specialised_met hod() {...} // 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
2 3814
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.

Cheers,
Lionel

--
Lionel B
Jul 25 '08 #3

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

Similar topics

6
1719
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 (add, add_confirm, edit, edit_confirm, view, delete). When the form starts out, it is in view mode....
0
1876
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 named MEMBER of type TD.
10
1775
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 {
5
3593
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:
2
2018
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 there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
4
4035
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 compile.
2
1586
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. I'd like to have some way of outputting my template first to the browser, and then within the...
9
3347
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
0
1954
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 template functions to one of my personal library modules, and I got this linker error message (from...
4
5078
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;
0
8761
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.