Boost logo

Boost Users :

From: Zeljko Vrba (zvrba_at_[hidden])
Date: 2007-06-22 12:31:34


I have the following situation with parallel overloaded functions:

void f(X *x) | void f(int g, X *x) {
{ | {
  Impl<false>::f(-1, x); | Impl<true>::f(g, x);
} | }

template<bool F>
struct Impl {
  typedef Detail<F> detail;

  void f(int g, X *x)
  {
    // CODE
        X *t = detail::smth(g, x);
        // CODE
  }
};

In the "false" case, the argument g of Impl::f is not used and I want to get
rid of it in compile-time as an optimization. The rest of the CODE is
completely identical.

In other words, I want to transform the "false" case, at compile-time, to
the following:

void f(X *x)
{
  Impl<false>::f(x);
}

template<>
struct Impl<false> {
  typedef Detail<false> detail;

  void f(X *x)
  {
    // CODE
        X *t = detail::smth(x);
        // CODE
  }
};

Now, I could manually duplicate all of the code in Impl::f(int, X*) into
Impl::f(X*) and remove the extra argument in the call to detail::smth.
Doing such specialization manually seems _very_ inelegant and error-prone
(I'm not really a fan of copy-pasting the code :)) Is there a better way;
ie. doing it automatically? If so, how?

It is acceptable to manually overload the top-level f() function, as it
has no other code except the dispatch to Impl::f.

I want full compile-time solution; ie. I do not want the stack space for the
unneeded integer parameter ever to be allocated. [The code for the "false"
case will be called on every function invocation, so YES, performance DOES
matter even without apriori profiling.]

Signatures in the implementation of Impl and Detail are flexible and I can
change them if it helps solving the problem. The only thing that I do not
want to change are the signatures of top-level f() function overloads.

Thanks,
  Zeljko.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net