472,374 Members | 1,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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_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
2 3720
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
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...
0
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...
10
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
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...
2
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. ...
4
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' ...
2
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...
9
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
0
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...
4
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() {...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.