Boost logo

Boost Users :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-06-22 14:56:35


----- Mensaje original -----
De: Zeljko Vrba <zvrba_at_[hidden]>
Fecha: Viernes, Junio 22, 2007 6:32 pm
Asunto: [Boost-users] How to "splice" function arguments?
Para: boost-users_at_[hidden]

> 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
> }
> };
>
[...]

Something like the following?

  struct X;

  template<bool> struct detail;
  template<> struct detail<true>
  {
    static X* smth(int,X*);
  };
  template<> struct detail<false>
  {
   static X * smth(X*);
  };

  template<bool> struct impl_arg;
  template<> struct impl_arg<true>
  {
    impl_arg(int g,X* x):g(g),x(x){}
    int g;
    X* x;
  };
  template<> struct impl_arg<false>
  {
    impl_arg(X* x):x(x){}
    X* x;
  };

  X* call_smth(impl_arg<true> arg)
  {
    return detail<true>::smth(arg.g,arg.x);
  }

  X* call_smth(impl_arg<false> arg)
  {
    return detail<false>::smth(arg.x);
  }

  template<bool F>
  struct impl
  {
    static void f(impl_arg<F> arg)
    {
      // code with arg.x
      call_smth(arg);
      // code with arg.x
    }
  };

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

  void f(int g,X* x)
  {
    impl<true>::f(impl_arg<true>(g,x));
  }

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


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