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

Discussion in 'C++' started by Lionel B, Jul 25, 2008.

  1. Lionel B

    Lionel B Guest

    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
    Lionel B, Jul 25, 2008
    #1
    1. Advertising

  2. Lionel B

    Lionel B Guest

    Re: A workable scheme for "template parameter-conditionalcompilation"?

    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
    Lionel B, Jul 25, 2008
    #2
    1. Advertising

  3. Lionel B

    Lionel B Guest

    Re: A workable scheme for "template parameter-conditionalcompilation"?

    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
    Lionel B, Jul 25, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jens Mander
    Replies:
    0
    Views:
    493
    Jens Mander
    Jun 10, 2005
  2. Jens Mander
    Replies:
    2
    Views:
    1,357
    Jerry Coffin
    Sep 1, 2005
  3. Joachim Smit
    Replies:
    2
    Views:
    953
    Joachim Smit
    Apr 23, 2004
  4. Steve - DND

    Implementing a Permissions Scheme

    Steve - DND, Mar 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    291
    Steve - DND
    Mar 5, 2004
  5. Manu Ashok
    Replies:
    1
    Views:
    87
    Michael Winter
    Apr 2, 2004
Loading...

Share This Page